UNPKG

6.53 kBJavaScriptView Raw
1/*
2Language: AsciiDoc
3Requires: xml.js
4Author: Dan Allen <dan.j.allen@gmail.com>
5Website: http://asciidoc.org
6Description: A semantic, text-based document format that can be exported to HTML, DocBook and other backends.
7Category: markup
8*/
9
10/** @type LanguageFn */
11function asciidoc(hljs) {
12 const regex = hljs.regex;
13 const HORIZONTAL_RULE = {
14 begin: '^\'{3,}[ \\t]*$',
15 relevance: 10
16 };
17 const ESCAPED_FORMATTING = [
18 // escaped constrained formatting marks (i.e., \* \_ or \`)
19 {
20 begin: /\\[*_`]/
21 },
22 // escaped unconstrained formatting marks (i.e., \\** \\__ or \\``)
23 // must ignore until the next formatting marks
24 // this rule might not be 100% compliant with Asciidoctor 2.0 but we are entering undefined behavior territory...
25 {
26 begin: /\\\\\*{2}[^\n]*?\*{2}/
27 },
28 {
29 begin: /\\\\_{2}[^\n]*_{2}/
30 },
31 {
32 begin: /\\\\`{2}[^\n]*`{2}/
33 },
34 // guard: constrained formatting mark may not be preceded by ":", ";" or
35 // "}". match these so the constrained rule doesn't see them
36 {
37 begin: /[:;}][*_`](?![*_`])/
38 }
39 ];
40 const STRONG = [
41 // inline unconstrained strong (single line)
42 {
43 className: 'strong',
44 begin: /\*{2}([^\n]+?)\*{2}/
45 },
46 // inline unconstrained strong (multi-line)
47 {
48 className: 'strong',
49 begin: regex.concat(
50 /\*\*/,
51 /((\*(?!\*)|\\[^\n]|[^*\n\\])+\n)+/,
52 /(\*(?!\*)|\\[^\n]|[^*\n\\])*/,
53 /\*\*/
54 ),
55 relevance: 0
56 },
57 // inline constrained strong (single line)
58 {
59 className: 'strong',
60 // must not precede or follow a word character
61 begin: /\B\*(\S|\S[^\n]*?\S)\*(?!\w)/
62 },
63 // inline constrained strong (multi-line)
64 {
65 className: 'strong',
66 // must not precede or follow a word character
67 begin: /\*[^\s]([^\n]+\n)+([^\n]+)\*/
68 }
69 ];
70 const EMPHASIS = [
71 // inline unconstrained emphasis (single line)
72 {
73 className: 'emphasis',
74 begin: /_{2}([^\n]+?)_{2}/
75 },
76 // inline unconstrained emphasis (multi-line)
77 {
78 className: 'emphasis',
79 begin: regex.concat(
80 /__/,
81 /((_(?!_)|\\[^\n]|[^_\n\\])+\n)+/,
82 /(_(?!_)|\\[^\n]|[^_\n\\])*/,
83 /__/
84 ),
85 relevance: 0
86 },
87 // inline constrained emphasis (single line)
88 {
89 className: 'emphasis',
90 // must not precede or follow a word character
91 begin: /\b_(\S|\S[^\n]*?\S)_(?!\w)/
92 },
93 // inline constrained emphasis (multi-line)
94 {
95 className: 'emphasis',
96 // must not precede or follow a word character
97 begin: /_[^\s]([^\n]+\n)+([^\n]+)_/
98 },
99 // inline constrained emphasis using single quote (legacy)
100 {
101 className: 'emphasis',
102 // must not follow a word character or be followed by a single quote or space
103 begin: '\\B\'(?![\'\\s])',
104 end: '(\\n{2}|\')',
105 // allow escaped single quote followed by word char
106 contains: [{
107 begin: '\\\\\'\\w',
108 relevance: 0
109 }],
110 relevance: 0
111 }
112 ];
113 const ADMONITION = {
114 className: 'symbol',
115 begin: '^(NOTE|TIP|IMPORTANT|WARNING|CAUTION):\\s+',
116 relevance: 10
117 };
118 const BULLET_LIST = {
119 className: 'bullet',
120 begin: '^(\\*+|-+|\\.+|[^\\n]+?::)\\s+'
121 };
122
123 return {
124 name: 'AsciiDoc',
125 aliases: ['adoc'],
126 contains: [
127 // block comment
128 hljs.COMMENT(
129 '^/{4,}\\n',
130 '\\n/{4,}$',
131 // can also be done as...
132 // '^/{4,}$',
133 // '^/{4,}$',
134 {
135 relevance: 10
136 }
137 ),
138 // line comment
139 hljs.COMMENT(
140 '^//',
141 '$',
142 {
143 relevance: 0
144 }
145 ),
146 // title
147 {
148 className: 'title',
149 begin: '^\\.\\w.*$'
150 },
151 // example, admonition & sidebar blocks
152 {
153 begin: '^[=\\*]{4,}\\n',
154 end: '\\n^[=\\*]{4,}$',
155 relevance: 10
156 },
157 // headings
158 {
159 className: 'section',
160 relevance: 10,
161 variants: [
162 {
163 begin: '^(={1,6})[ \t].+?([ \t]\\1)?$'
164 },
165 {
166 begin: '^[^\\[\\]\\n]+?\\n[=\\-~\\^\\+]{2,}$'
167 }
168 ]
169 },
170 // document attributes
171 {
172 className: 'meta',
173 begin: '^:.+?:',
174 end: '\\s',
175 excludeEnd: true,
176 relevance: 10
177 },
178 // block attributes
179 {
180 className: 'meta',
181 begin: '^\\[.+?\\]$',
182 relevance: 0
183 },
184 // quoteblocks
185 {
186 className: 'quote',
187 begin: '^_{4,}\\n',
188 end: '\\n_{4,}$',
189 relevance: 10
190 },
191 // listing and literal blocks
192 {
193 className: 'code',
194 begin: '^[\\-\\.]{4,}\\n',
195 end: '\\n[\\-\\.]{4,}$',
196 relevance: 10
197 },
198 // passthrough blocks
199 {
200 begin: '^\\+{4,}\\n',
201 end: '\\n\\+{4,}$',
202 contains: [{
203 begin: '<',
204 end: '>',
205 subLanguage: 'xml',
206 relevance: 0
207 }],
208 relevance: 10
209 },
210
211 BULLET_LIST,
212 ADMONITION,
213 ...ESCAPED_FORMATTING,
214 ...STRONG,
215 ...EMPHASIS,
216
217 // inline smart quotes
218 {
219 className: 'string',
220 variants: [
221 {
222 begin: "``.+?''"
223 },
224 {
225 begin: "`.+?'"
226 }
227 ]
228 },
229 // inline unconstrained emphasis
230 {
231 className: 'code',
232 begin: /`{2}/,
233 end: /(\n{2}|`{2})/
234 },
235 // inline code snippets (TODO should get same treatment as strong and emphasis)
236 {
237 className: 'code',
238 begin: '(`.+?`|\\+.+?\\+)',
239 relevance: 0
240 },
241 // indented literal block
242 {
243 className: 'code',
244 begin: '^[ \\t]',
245 end: '$',
246 relevance: 0
247 },
248 HORIZONTAL_RULE,
249 // images and links
250 {
251 begin: '(link:)?(http|https|ftp|file|irc|image:?):\\S+?\\[[^[]*?\\]',
252 returnBegin: true,
253 contains: [
254 {
255 begin: '(link|image:?):',
256 relevance: 0
257 },
258 {
259 className: 'link',
260 begin: '\\w',
261 end: '[^\\[]+',
262 relevance: 0
263 },
264 {
265 className: 'string',
266 begin: '\\[',
267 end: '\\]',
268 excludeBegin: true,
269 excludeEnd: true,
270 relevance: 0
271 }
272 ],
273 relevance: 10
274 }
275 ]
276 };
277}
278
279export { asciidoc as default };