gatsby-remark-reference-footnotes
Advanced tools
Comparing version 0.0.1 to 0.0.2
205
index.js
@@ -1,3 +0,1 @@ | ||
// var util = require("mdast-util-toc"); | ||
// const fs = require("fs"); | ||
const yaml = require("js-yaml"); | ||
@@ -48,5 +46,9 @@ const visit = require("unist-util-visit"); | ||
} | ||
return `<sup class="footnote-inline" id="use-ref-${ref}">${prefs.inlineTextPrefix ? prefs.inlineTextPrefix : ""}<a href="#ref-${ref}" class="footnote-inline-link">${ | ||
return `<sup class="footnote-inline" id="use-ref-${ref}">${ | ||
prefs.inlineTextPrefix ? prefs.inlineTextPrefix : "" | ||
}<a href="#ref-${ref}" class="footnote-inline-link">${ | ||
prefs.inlineLinkPrefix ? prefs.inlineLinkPrefix : "" | ||
}${label}${prefs.inlineLinkSuffix ? prefs.inlineLinkSuffix : ""}</a>${prefs.inlineTextSuffix ? prefs.inlineTextSuffix : ""}</sup>`; | ||
}${label}${prefs.inlineLinkSuffix ? prefs.inlineLinkSuffix : ""}</a>${ | ||
prefs.inlineTextSuffix ? prefs.inlineTextSuffix : "" | ||
}</sup>`; | ||
}; | ||
@@ -66,5 +68,3 @@ | ||
prefs.referenceLinkPrefix ? prefs.referenceLinkPrefix : "" | ||
}${label}${ | ||
prefs.referenceLinkSuffix ? prefs.referenceLinkSuffix : "" | ||
}</a>${ | ||
}${label}${prefs.referenceLinkSuffix ? prefs.referenceLinkSuffix : ""}</a>${ | ||
prefs.referenceTextSuffix ? prefs.referenceTextSuffix : "" | ||
@@ -99,17 +99,50 @@ }</span>`; | ||
// final footnotes | ||
const footnotes = {}; | ||
const footnotes = []; | ||
// change the inline footnotes | ||
// register the inline footnotes | ||
visit(markdownAST, `footnote`, node => { | ||
let { group, text } = getTextAndGroup(node.children[0].value); | ||
if (prefs.groupInclude === group) { | ||
let newChildren = node.children; | ||
newChildren[0].value = text; | ||
footnotes[group] = footnotes[group] || []; | ||
footnotes[group].push({ | ||
footnotes.push({ | ||
type: "footnote", | ||
children: newChildren | ||
children: node.children, | ||
// identifier set like a group so it can never clash | ||
identifier: `:foootnote:--${node.position.start.offset}`, | ||
offset: node.position.start.offset | ||
}); | ||
let index = footnotes[group].length; | ||
} | ||
}); | ||
// register the footnote references | ||
visit(markdownAST, `footnoteReference`, node => { | ||
let { group, text } = getTextAndGroup(node.identifier); | ||
if (prefs.groupInclude === group) { | ||
footnotes.push({ | ||
type: "footnoteReference", | ||
identifier: node.identifier, | ||
offset: node.position.start.offset | ||
}); | ||
} | ||
}); | ||
// sort footnotes | ||
footnotes.sort((a, b) => a.offset > b.offset); | ||
// filter by identifier | ||
let uniqueFootnotes = footnotes.filter( | ||
(item, i, ar) => ar.findIndex(fn => fn.identifier === item.identifier) === i | ||
); | ||
// change the inline footnotes | ||
visit(markdownAST, `footnote`, node => { | ||
let identifier = `:foootnote:--${node.position.start.offset}`; | ||
let { group, text } = getTextAndGroup(node.children[0].value); | ||
if (prefs.groupInclude === group) { | ||
// cut off group-notation | ||
node.children[0].value = text; | ||
// find the index | ||
let index = | ||
uniqueFootnotes.findIndex(item => item.identifier === identifier) + 1; | ||
replaceRefNode({ node, group, index, prefs }); | ||
@@ -121,10 +154,8 @@ } | ||
visit(markdownAST, `footnoteReference`, node => { | ||
let identifier = node.identifier; | ||
let { group, text } = getTextAndGroup(node.identifier); | ||
if (prefs.groupInclude === group) { | ||
footnotes[group] = footnotes[group] || []; | ||
footnotes[group].push({ | ||
type: "footnoteReference", | ||
identifier: node.identifier | ||
}); | ||
let index = footnotes[group].length; | ||
// find the index | ||
let index = | ||
uniqueFootnotes.findIndex(item => item.identifier === identifier) + 1; | ||
@@ -135,82 +166,70 @@ replaceRefNode({ node, group, index, prefs }); | ||
// the final references in the output | ||
let printReferences = []; | ||
// the list to output | ||
let list = { | ||
type: "list", | ||
spread: true, // add p tag arround content | ||
children: [] | ||
}; | ||
Object.keys(footnotes) | ||
.filter(item => item === prefs.groupInclude) | ||
.forEach(key => { | ||
let footnoteList = footnotes[key]; | ||
uniqueFootnotes.forEach((footnote, footnoteIndex) => { | ||
let content = []; | ||
let list = { | ||
type: "list", | ||
spread: true, // add p tag arround content | ||
children: [] | ||
}; | ||
let renderLinkRef = renderRef({ | ||
index: `${footnoteIndex + 1}`, | ||
label: `${footnoteIndex + 1}`, | ||
group: prefs.groupInclude, | ||
prefs | ||
}); | ||
footnoteList.forEach((footnote, footnoteIndex) => { | ||
let content = []; | ||
if (footnote.type === "footnote") { | ||
content.push({ | ||
type: "paragraph", | ||
children: [...footnote.children] | ||
}); | ||
} | ||
if (footnote.type === "footnoteReference") { | ||
let def = footnoteDefinitions.find( | ||
item => item.identifier === footnote.identifier | ||
); | ||
if (def) { | ||
content.push(...def.children); | ||
} | ||
} | ||
let renderLinkRef = renderRef({ | ||
index: `${footnoteIndex + 1}`, | ||
label: `${footnoteIndex + 1}`, | ||
group: prefs.groupInclude, | ||
prefs | ||
// add the back-reference | ||
if (content.length > 0) { | ||
if ((prefs.referenceLinkPosition || "").toLowerCase() === "end") { | ||
content[0].children.push({ | ||
type: "html", | ||
value: renderLinkRef | ||
}); | ||
} else { | ||
content[0].children = [ | ||
{ | ||
type: "html", | ||
value: renderLinkRef | ||
}, | ||
...content[0].children | ||
]; | ||
} | ||
if (footnote.type === "footnote") { | ||
content.push({ | ||
type: "paragraph", | ||
children: [...footnote.children] | ||
}); | ||
} | ||
if (footnote.type === "footnoteReference") { | ||
let def = footnoteDefinitions.find( | ||
item => item.identifier === footnote.identifier | ||
); | ||
if (def) { | ||
content.push(...def.children); | ||
} | ||
} | ||
// add the back-reference | ||
if (content.length > 0) { | ||
if ((prefs.referenceLinkPosition || "").toLowerCase() === "end") { | ||
content[0].children.push({ | ||
type: "html", | ||
value: renderLinkRef | ||
}); | ||
} else { | ||
content[0].children = [ | ||
{ | ||
type: "html", | ||
value: renderLinkRef | ||
}, | ||
...content[0].children | ||
]; | ||
} | ||
// add list item | ||
list.children.push({ | ||
type: "listItem", | ||
children: content | ||
}); | ||
} | ||
// add list item | ||
list.children.push({ | ||
type: "listItem", | ||
children: content | ||
}); | ||
// wrapper arround the whole list -- start | ||
printReferences.push({ | ||
type: "html", | ||
value: `<div class="ref-notes refnotes--${prefs.groupInclude}">` | ||
}); | ||
} | ||
}); | ||
printReferences.push(list); | ||
// wrapper arround the whole list -- end | ||
printReferences.push({ | ||
type: "html", | ||
value: `</div>` | ||
}); | ||
}); | ||
markdownAST.children = [].concat( | ||
markdownAST.children.slice(0, index), | ||
printReferences, | ||
{ | ||
type: "html", | ||
value: `<div class="ref-notes refnotes--${prefs.groupInclude}">` | ||
}, | ||
list, | ||
{ | ||
type: "html", | ||
value: `</div>` | ||
}, | ||
markdownAST.children.slice(index + 1) | ||
@@ -221,4 +240,2 @@ ); | ||
const transformer = (markdownAST, pluginOptions) => { | ||
// fs.writeFileSync("./data-before.json", JSON.stringify(markdownAST, null, 2)); | ||
// transform backwards all refs-code blocks | ||
@@ -247,4 +264,2 @@ for (let i = markdownAST.children.length - 1; i >= 0; --i) { | ||
} | ||
// fs.writeFileSync("./data-after.json", JSON.stringify(markdownAST, null, 2)); | ||
}; | ||
@@ -251,0 +266,0 @@ |
{ | ||
"name": "gatsby-remark-reference-footnotes", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "gatsby remark plugin to generate footnotes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -142,17 +142,12 @@ # Footnotes in Gatsby | ||
````md | ||
Text with inline footnote[^here the reference]. | ||
Text with[^test] reference footnote[^test]. | ||
Text with inline footnote[^here the inline reference]. | ||
![Cat](http://placekitten.com/g/200/300) | ||
*This is a cat [^:fig:From placekitten.com]* | ||
![Cat](http://placekitten.com/g/80/120) | ||
*This is a cat [^:fig:Reference from a different group]* | ||
Text with reference footnote[^test]. | ||
<!-- refs --> | ||
[^test]: This is the named reference | ||
[^:fig:test]: This is the named reference | ||
## Footnotes | ||
### Footnotes | ||
```references | ||
@@ -162,4 +157,3 @@ # gets repalced with footnotes | ||
## Figures | ||
### Figures | ||
```references | ||
@@ -177,23 +171,3 @@ # gets repalced with footnotes | ||
### Output | ||
<p>Text with inline footnote<sup class="footnote-inline" id="use-ref-1"><a href="#ref-1" class="footnote-inline-link">1</a></sup>.</p> | ||
<p><img src="http://placekitten.com/g/200/300" alt="Cat"> | ||
<em>This is a cat <sup class="footnote-inline" id="use-ref-fig-1"><a href="#ref-fig-1" class="footnote-inline-link">Fig. 1</a></sup></em></p> | ||
<p>Text with reference footnote<sup class="footnote-inline" id="use-ref-2"><a href="#ref-2" class="footnote-inline-link">2</a></sup>.</p> | ||
<!-- refs --> | ||
<h2>Footnotes</h2> | ||
<div class="ref-notes refnotes--default"> | ||
<ul> | ||
<li> | ||
<p><span class="footnote-ref" id="ref-1"><a id="use-ref-1" href="#use-ref-1" class="footnote-ref-link">ā 1.</a> </span>here the reference</p> | ||
</li> | ||
</ul> | ||
</div> | ||
<h2>Figures</h2> | ||
<div class="ref-notes refnotes--fig"> | ||
<ul> | ||
<li> | ||
<p>From placekitten.com<span class="footnote-ref" id="ref-fig-1"> <a id="use-ref-fig-1" href="#use-ref-fig-1" class="footnote-ref-link"> Fig. 1 ā”</a> </span></p> | ||
</li> | ||
</ul> | ||
</div> | ||
![example output](./example/doc.png) | ||
@@ -200,0 +174,0 @@ ## License |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
233
17777
184
1