-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.js
62 lines (49 loc) · 1.48 KB
/
markdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { h } from './lib/h.js'
import { bogbot } from './bogbot.js'
import { gossip } from './gossip.js'
import { marked } from './lib/marked.esm.js'
const renderer = new marked.Renderer()
renderer.paragraph = function (paragraph) {
const array = paragraph.split(' ')
for (let i = 0; i < array.length; i++) {
let word = array[i]
if (word.startsWith('#')) {
let end
if (['.', ',', '?', ':', '!'].some(char => word.endsWith(char))) {
end = word[word.length - 1]
word = word.substring(0, word.length - 1)
}
const hashtag = "<a href='#?" + word + "'>" + word + "</a>"
if (end) {
hashtag = hashtag + end
}
array[i] = hashtag
}
}
const newgraph = array.join(' ')
return '<p>' + newgraph + '</p>'
}
renderer.link = function (href, title, text) {
if (href.length == 44 && !href.startsWith('http')) {
href = '#' + href
return marked.Renderer.prototype.link.call(this, href, title, text);
} else {
return marked.Renderer.prototype.link.call(this, href, title, text);
}
}
renderer.image = async function (src, unknown, title) {
if (src.length === 44) {
const file = await bogbot.find(src)
if (file) {
const div = document.getElementById(src)
div.src = file
}
return '<div><img id="' + src + '" title="' + title + '" class="thumb" /></div>'
}
}
marked.setOptions({
renderer: renderer
})
export const markdown = async (txt) => {
return '<p>' + marked(txt) + '</p>'
}