|
1 | 1 | import attrs from 'markdown-it-attrs' |
2 | 2 | import md from 'markdown-it' |
| 3 | +import type { Token } from 'markdown-it' |
3 | 4 | import blockEmbed from 'markdown-it-block-embed' |
4 | 5 | import multimdTable from 'markdown-it-multimd-table' |
5 | 6 | import taskLists from 'markdown-it-task-lists' |
@@ -54,23 +55,26 @@ const note = (markdown, config) => { |
54 | 55 | } |
55 | 56 |
|
56 | 57 | // tslint:disable-next-line: only-arrow-functions |
57 | | - markdown.renderer.rules.paragraph_open = function (tokens: any[], idx, options, env, self) { |
58 | | - const inlineToken = tokens[idx + 1] // text |
| 58 | + markdown.renderer.rules.paragraph_open = function (tokens: Token[], idx, options, env, self) { |
| 59 | + const currentToken = tokens[idx]! |
| 60 | + const inlineToken = tokens[idx + 1]! // text |
59 | 61 | if (inlineToken.content.startsWith(notesSeparator)) { |
60 | | - tokens[idx].tag = 'aside' |
61 | | - const classIndex = tokens[idx].attrIndex('class') |
| 62 | + currentToken.tag = 'aside' |
| 63 | + const classIndex = currentToken.attrIndex('class') |
62 | 64 |
|
63 | 65 | if (classIndex < 0) { |
64 | | - tokens[idx].attrPush(['class', notesClass]) // add new attribute |
65 | | - } else { |
66 | | - tokens[idx].attrs[classIndex][1] = notesClass // replace value of existing attr |
| 66 | + currentToken.attrPush(['class', notesClass]) // add new attribute |
| 67 | + } else if (currentToken.attrs) { |
| 68 | + currentToken.attrs[classIndex][1] = notesClass // replace value of existing attr |
67 | 69 | } |
68 | 70 |
|
69 | 71 | // remote "note:" from content |
70 | | - tokens[idx + 1].content = inlineToken.content.replace(notesSeparator, '') |
71 | | - tokens[idx + 1].children[0].content = tokens[idx + 1].children[0].content.replace(notesSeparator, '') |
| 72 | + inlineToken.content = inlineToken.content.replace(notesSeparator, '') |
| 73 | + if (inlineToken.children && inlineToken.children[0]) { |
| 74 | + inlineToken.children[0].content = inlineToken.children[0].content.replace(notesSeparator, '') |
| 75 | + } |
72 | 76 |
|
73 | | - tokens[idx + 2].tag = 'aside' |
| 77 | + tokens[idx + 2]!.tag = 'aside' |
74 | 78 | } |
75 | 79 | // pass token to default renderer. |
76 | 80 | return defaultRender(tokens, idx, options, env, self) |
|
0 commit comments