forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturndownService.js
More file actions
65 lines (53 loc) · 1.95 KB
/
turndownService.js
File metadata and controls
65 lines (53 loc) · 1.95 KB
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
62
63
64
65
import TurndownService from 'turndown'
import { identity } from './index'
const turndownPluginGfm = require('joplin-turndown-plugin-gfm')
export const usePluginAddRules = (turndownService, keeps) => {
// Use the gfm plugin
const { gfm } = turndownPluginGfm
turndownService.use(gfm)
// We need a extra strikethrough rule because the strikethrough rule in gfm is single `~`.
turndownService.addRule('strikethrough', {
filter: ['del', 's', 'strike'],
replacement (content) {
return '~~' + content + '~~'
}
})
turndownService.addRule('paragraph', {
filter: 'p',
replacement: function (content, node) {
const isTaskListItemParagraph = node.previousElementSibling && node.previousElementSibling.tagName === 'INPUT'
return isTaskListItemParagraph ? content + '\n\n' : '\n\n' + content + '\n\n'
}
})
turndownService.addRule('listItem', {
filter: 'li',
replacement: function (content, node, options) {
content = content
.replace(/^\n+/, '') // remove leading newlines
.replace(/\n+$/, '\n') // replace trailing newlines with just a single one
.replace(/\n/gm, '\n ') // indent
let prefix = options.bulletListMarker + ' '
const parent = node.parentNode
if (parent.nodeName === 'OL') {
const start = parent.getAttribute('start')
const index = Array.prototype.indexOf.call(parent.children, node)
prefix = (start ? Number(start) + index : index + 1) + '. '
}
return (
prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
)
}
})
// Handle multiple math lines
turndownService.addRule('multiplemath', {
filter (node, options) {
return node.nodeName === 'PRE' && node.classList.contains('multiple-math')
},
replacement (content, node, options) {
return `$$\n${content}\n$$`
}
})
turndownService.escape = identity
turndownService.keep(keeps)
}
export default TurndownService