Debian Codegraph @

src/commonmark-rules.js

raw symbols SCIP
1 import { escapeMarkdown, repeat, trimNewlines } from './utilities'
2
3 const rules = {}
4
5 rules.paragraph = {
6 filter: 'p',
7
8 replacement: function (content) {
9 return '\n\n' + content + '\n\n'
10 }
11 }
12
13 rules.lineBreak = {
14 filter: 'br',
15
16 replacement: function (content, node, options) {
17 return options.br + '\n'
18 }
19 }
20
21 rules.heading = {
22 filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
23
24 replacement: function (content, node, options) {
25 const hLevel = Number(node.nodeName.charAt(1))
26
27 if (options.headingStyle === 'setext' && hLevel < 3) {
28 const underline = repeat((hLevel === 1 ? '=' : '-'), content.length)
29 return (
30 '\n\n' + content + '\n' + underline + '\n\n'
31 )
32 } else {
33 return '\n\n' + repeat('#', hLevel) + ' ' + content + '\n\n'
34 }
35 }
36 }
37
38 rules.blockquote = {
39 filter: 'blockquote',
40
41 replacement: function (content) {
42 content = trimNewlines(content).replace(/^/gm, '> ')
43 return '\n\n' + content + '\n\n'
44 }
45 }
46
47 rules.list = {
48 filter: ['ul', 'ol'],
49
50 replacement: function (content, node) {
51 const parent = node.parentNode
52 if (parent.nodeName === 'LI' && parent.lastElementChild === node) {
53 return '\n' + content
54 } else {
55 return '\n\n' + content + '\n\n'
56 }
57 }
58 }
59
60 rules.listItem = {
61 filter: 'li',
62
63 replacement: function (content, node, options) {
64 let prefix = options.bulletListMarker + ' '
65 const parent = node.parentNode
66 if (parent.nodeName === 'OL') {
67 const start = parent.getAttribute('start')
68 const index = Array.prototype.indexOf.call(parent.children, node)
69 prefix = (start ? Number(start) + index : index + 1) + '. '
70 }
71 const isParagraph = /\n$/.test(content)
72 content = trimNewlines(content) + (isParagraph ? '\n' : '')
73 content = content.replace(/\n/gm, '\n' + ' '.repeat(prefix.length)) // indent
74 return (
75 prefix + content + (node.nextSibling ? '\n' : '')
76 )
77 }
78 }
79
80 rules.indentedCodeBlock = {
81 filter: function (node, options) {
82 return (
83 options.codeBlockStyle === 'indented' &&
84 node.nodeName === 'PRE' &&
85 node.firstChild &&
86 node.firstChild.nodeName === 'CODE'
87 )
88 },
89
90 replacement: function (content, node, options) {
91 return (
92 '\n\n ' +
93 node.firstChild.textContent.replace(/\n/g, '\n ') +
94 '\n\n'
95 )
96 }
97 }
98
99 rules.fencedCodeBlock = {
100 filter: function (node, options) {
101 return (
102 options.codeBlockStyle === 'fenced' &&
103 node.nodeName === 'PRE' &&
104 node.firstChild &&
105 node.firstChild.nodeName === 'CODE'
106 )
107 },
108
109 replacement: function (content, node, options) {
110 const className = node.firstChild.getAttribute('class') || ''
111 const language = (className.match(/language-(\S+)/) || [null, ''])[1]
112 const code = node.firstChild.textContent
113
114 const fenceChar = options.fence.charAt(0)
115 let fenceSize = 3
116 const fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm')
117
118 let match
119 while ((match = fenceInCodeRegex.exec(code))) {
120 if (match[0].length >= fenceSize) {
121 fenceSize = match[0].length + 1
122 }
123 }
124
125 const fence = repeat(fenceChar, fenceSize)
126
127 return (
128 '\n\n' + fence + language + '\n' +
129 code.replace(/\n$/, '') +
130 '\n' + fence + '\n\n'
131 )
132 }
133 }
134
135 rules.horizontalRule = {
136 filter: 'hr',
137
138 replacement: function (content, node, options) {
139 return '\n\n' + options.hr + '\n\n'
140 }
141 }
142
143 rules.inlineLink = {
144 filter: function (node, options) {
145 return (
146 options.linkStyle === 'inlined' &&
147 node.nodeName === 'A' &&
148 node.getAttribute('href')
149 )
150 },
151
152 replacement: function (content, node) {
153 const href = escapeLinkDestination(node.getAttribute('href'))
154 const title = escapeLinkTitle(cleanAttribute(node.getAttribute('title')))
155 const titlePart = title ? ' "' + title + '"' : ''
156 return '[' + content + '](' + href + titlePart + ')'
157 }
158 }
159
160 rules.referenceLink = {
161 filter: function (node, options) {
162 return (
163 options.linkStyle === 'referenced' &&
164 node.nodeName === 'A' &&
165 node.getAttribute('href')
166 )
167 },
168
169 replacement: function (content, node, options) {
170 const href = escapeLinkDestination(node.getAttribute('href'))
171 let title = cleanAttribute(node.getAttribute('title'))
172 if (title) title = ' "' + escapeLinkTitle(title) + '"'
173 let replacement
174 let reference
175
176 switch (options.linkReferenceStyle) {
177 case 'collapsed':
178 replacement = '[' + content + '][]'
179 reference = '[' + content + ']: ' + href + title
180 break
181 case 'shortcut':
182 replacement = '[' + content + ']'
183 reference = '[' + content + ']: ' + href + title
184 break
185 default:
186 var id = this.references.length + 1
187 replacement = '[' + content + '][' + id + ']'
188 reference = '[' + id + ']: ' + href + title
189 }
190
191 this.references.push(reference)
192 return replacement
193 },
194
195 references: [],
196
197 append: function (options) {
198 let references = ''
199 if (this.references.length) {
200 references = '\n\n' + this.references.join('\n') + '\n\n'
201 this.references = [] // Reset references
202 }
203 return references
204 }
205 }
206
207 rules.emphasis = {
208 filter: ['em', 'i'],
209
210 replacement: function (content, node, options) {
211 if (!content.trim()) return ''
212 return options.emDelimiter + content + options.emDelimiter
213 }
214 }
215
216 rules.strong = {
217 filter: ['strong', 'b'],
218
219 replacement: function (content, node, options) {
220 if (!content.trim()) return ''
221 return options.strongDelimiter + content + options.strongDelimiter
222 }
223 }
224
225 rules.code = {
226 filter: function (node) {
227 const hasSiblings = node.previousSibling || node.nextSibling
228 const isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings
229
230 return node.nodeName === 'CODE' && !isCodeBlock
231 },
232
233 replacement: function (content) {
234 if (!content) return ''
235 content = content.replace(/\r?\n|\r/g, ' ')
236
237 const extraSpace = /^`|^ .*?[^ ].* $|`$/.test(content) ? ' ' : ''
238 let delimiter = '`'
239 const matches = content.match(/`+/gm) || []
240 while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`'
241
242 return delimiter + extraSpace + content + extraSpace + delimiter
243 }
244 }
245
246 rules.image = {
247 filter: 'img',
248
249 replacement: function (content, node) {
250 const alt = escapeMarkdown(cleanAttribute(node.getAttribute('alt')))
251 const src = escapeLinkDestination(node.getAttribute('src') || '')
252 const title = cleanAttribute(node.getAttribute('title'))
253 const titlePart = title ? ' "' + escapeLinkTitle(title) + '"' : ''
254 return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
255 }
256 }
257
258 function cleanAttribute (attribute) {
259 return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : ''
260 }
261
262 function escapeLinkDestination (destination) {
263 const escaped = destination.replace(/([<>()])/g, '\\$1')
264 return escaped.indexOf(' ') >= 0 ? '<' + escaped + '>' : escaped
265 }
266
267 function escapeLinkTitle (title) {
268 return title.replace(/"/g, '\\"')
269 }
270
271 export default rules