Skip to content

Commit 7fafc2b

Browse files
authored
Enhance footnote handling by protecting code blocks in markdown (#6)
1 parent 7484d67 commit 7fafc2b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sy-records/docsify-footnotes",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "A plugin that supports the use of footnotes in docsify.",
55
"main": "dist/index.js",
66
"repository": {

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@ document.addEventListener('click', function (event) {
88

99
function footnotes(hook) {
1010
hook.beforeEach((markdown) => {
11-
return markdown.replace(/\[\^([A-Za-z0-9\-]+)\](\:)?/gm, (_, id, isDefinition) =>
11+
const codeBlocks = [];
12+
const placeholder = (i) => `{{CODE${i}}}`;
13+
14+
const protectedMarkdown = markdown.replace(/(```[\s\S]*?```|`[^`\n]+`)/g, (match) => {
15+
const index = codeBlocks.length;
16+
codeBlocks.push(match);
17+
return placeholder(index);
18+
});
19+
20+
console.error(protectedMarkdown)
21+
22+
const processed = protectedMarkdown.replace(/\[\^([A-Za-z0-9\-]+)\](\:)?/gm, (_, id, isDefinition) =>
1223
isDefinition
1324
? `<strong class="footnote-reference-symbol" data-ref="fn-${id}" id="fnref-${id}">[${id}](#fn-${id})</strong>:leftwards_arrow_with_hook: `
1425
: `<sup class="footnote-symbol" data-ref="fnref-${id}" id="fn-${id}">[${id}](#fnref-${id})</sup>`
1526
);
27+
28+
return processed.replace(/\{\{CODE(\d+)\}\}/g, (_, index) => codeBlocks[+index]);
1629
});
1730
}
1831

0 commit comments

Comments
 (0)