@amanda-mitchell/remark-renumber-footnotes
Advanced tools
Comparing version 2.0.0 to 3.0.0
101
index.js
@@ -1,100 +0,1 @@ | ||
import is from 'unist-util-is'; | ||
const isFootnoteReference = is.convert('footnoteReference'); | ||
const isFootnoteDefinition = is.convert('footnoteDefinition'); | ||
const isFootnote = is.convert('footnote'); | ||
function findFootnoteNodes(node) { | ||
if ( | ||
isFootnote(node) || | ||
isFootnoteReference(node) || | ||
isFootnoteDefinition(node) | ||
) { | ||
return [node]; | ||
} | ||
if (!node.children) { | ||
return []; | ||
} | ||
return node.children.flatMap(findFootnoteNodes); | ||
} | ||
function isNumeric(text) { | ||
return /^\d+$/.test(text); | ||
} | ||
function applyNewIdentifiers(node, remappedIdentifiers) { | ||
if (isFootnoteReference(node) || isFootnoteDefinition(node)) { | ||
const identifier = remappedIdentifiers[node.identifier]; | ||
if (!identifier) { | ||
return node; | ||
} | ||
return { ...node, identifier, label: identifier }; | ||
} | ||
if (!node.children) { | ||
return node; | ||
} | ||
return { | ||
...node, | ||
children: node.children.map(child => | ||
applyNewIdentifiers(child, remappedIdentifiers) | ||
), | ||
}; | ||
} | ||
export function renumberFootnotes(options) { | ||
const { ignoreNonnumericFootnotes } = options || { | ||
ignoreNonnumericFootnotes: false, | ||
}; | ||
return function transform(tree) { | ||
const nodes = findFootnoteNodes(tree); | ||
let identifierCount = 0; | ||
const remappedIdentifiers = {}; | ||
for (const node of nodes) { | ||
if (isFootnoteDefinition(node)) { | ||
continue; | ||
} | ||
if (isFootnote(node)) { | ||
// Footnote nodes don't get an identifier until rendered | ||
// to HTML, but we still need to account for them in our | ||
// numbering. | ||
++identifierCount; | ||
continue; | ||
} | ||
const identifier = node.identifier; | ||
if (ignoreNonnumericFootnotes && !isNumeric(identifier)) { | ||
continue; | ||
} | ||
if (!remappedIdentifiers[identifier]) { | ||
remappedIdentifiers[identifier] = (++identifierCount).toString(); | ||
} | ||
} | ||
const orphanedDefinitions = nodes.filter( | ||
node => | ||
isFootnoteDefinition(node) && | ||
!remappedIdentifiers[node.identifier] && | ||
(!ignoreNonnumericFootnotes || isNumeric(node.identifier)) | ||
); | ||
for (const node of orphanedDefinitions) { | ||
// Two definitions might use the same identifier, so we still need | ||
// to perform this check. | ||
if (!remappedIdentifiers[node.identifier]) { | ||
remappedIdentifiers[node.identifier] = (++identifierCount).toString(); | ||
} | ||
} | ||
return applyNewIdentifiers(tree, remappedIdentifiers); | ||
}; | ||
} | ||
export * from './dist/index.js'; |
{ | ||
"name": "@amanda-mitchell/remark-renumber-footnotes", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "A remark-parse plugin that renumbers footnotes sequentially.", | ||
@@ -22,14 +22,24 @@ "main": "index.js", | ||
"type": "module", | ||
"files": [ | ||
"index.js", | ||
"dist/**/*.js", | ||
"dist/**/*.map", | ||
"dist/**/*.d.ts", | ||
"!**/__tests__" | ||
], | ||
"types": "dist/index.d.ts", | ||
"devDependencies": { | ||
"@amanda-mitchell/node-project-scripts": "^3.4.0", | ||
"jest": "^27.0.6", | ||
"remark-footnotes": "^3.0.0", | ||
"remark-html": "^13.0.1", | ||
"remark-parse": "^9.0.0", | ||
"unified": "^9.2.0", | ||
"unist-builder": "^2.0.3" | ||
"@amanda-mitchell/node-project-scripts": "4.72.1", | ||
"@types/unist": "^3.0.2", | ||
"jest": "^29.0.0", | ||
"mdast": "^3.0.0", | ||
"remark-html": "^16.0.1", | ||
"remark-parse": "^11.0.0", | ||
"unified": "^11.0.2", | ||
"unist-builder": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"unist-util-is": "^4.0.4" | ||
"remark-gfm": "^4.0.0", | ||
"unist-util-is": "^6.0.0" | ||
} | ||
} |
@@ -13,3 +13,3 @@ # @amanda-mitchell/remark-renumber-footnotes | ||
Construct a unified parser and pass `renumerFooters` to it as a plugin: | ||
Construct a unified parser and pass `renumberFootnotes` to it as a plugin: | ||
@@ -19,3 +19,3 @@ ```js | ||
const markdown = require('remark-parse'); | ||
const footnotes = require('remark-footnotes'); | ||
const remarkGfm = require('remark-gfm'); | ||
const { | ||
@@ -28,3 +28,3 @@ renumberFootnotes, | ||
This is a document[^2] with[^2] a couple[^1] of^[inline] footnotes[^foo]. | ||
This is a document[^2] with[^2] a couple[^1] of footnotes[^foo]. | ||
@@ -38,3 +38,3 @@ [^foo]: Last item | ||
.use(markdown) | ||
.use(footnotes, { inlineNotes: true }) | ||
.use(remarkGfm) | ||
.use(renumberFootnotes) | ||
@@ -51,12 +51,16 @@ .use(html) | ||
```html | ||
<p>This is a document<sup id="fnref-1"><a href="#fn-1" class="footnote-ref">1</a></sup> with<sup id="fnref-1"><a href="#fn-1" class="footnote-ref">1</a></sup> a couple<sup id="fnref-2"><a href="#fn-2" class="footnote-ref">2</a></sup> of<sup id="fnref-3"><a href="#fn-3" class="footnote-ref">3</a></sup> footnotes<sup id="fnref-4"><a href="#fn-4" class="footnote-ref">4</a></sup>.</p> | ||
<div class="footnotes"> | ||
<hr> | ||
<p>This is a document<sup><a href="#user-content-fn-1" id="user-content-user-content-fnref-1" data-footnote-ref aria-describedby="user-content-footnote-label">1</a></sup> with<sup><a href="#user-content-fn-1" id="user-content-user-content-fnref-1-2" data-footnote-ref aria-describedby="user-content-footnote-label">1</a></sup> a couple<sup><a href="#user-content-fn-2" id="user-content-user-content-fnref-2" data-footnote-ref aria-describedby="user-content-footnote-label">2</a></sup> of footnotes<sup><a href="#user-content-fn-3" id="user-content-user-content-fnref-3" data-footnote-ref aria-describedby="user-content-footnote-label">3</a></sup>.</p> | ||
<section data-footnotes class="footnotes"><h2 class="sr-only" id="user-content-footnote-label">Footnotes</h2> | ||
<ol> | ||
<li id="fn-1">First<a href="#fnref-1" class="footnote-backref">↩</a></li> | ||
<li id="fn-2">Second<a href="#fnref-2" class="footnote-backref">↩</a></li> | ||
<li id="fn-3">inline<a href="#fnref-3" class="footnote-backref">↩</a></li> | ||
<li id="fn-4">Last item<a href="#fnref-4" class="footnote-backref">↩</a></li> | ||
<li id="user-content-user-content-fn-1"> | ||
<p>First <a href="#user-content-fnref-1" data-footnote-backref="" aria-label="Back to reference 1" class="data-footnote-backref">↩</a> <a href="#user-content-fnref-1-2" data-footnote-backref="" aria-label="Back to reference 1-2" class="data-footnote-backref">↩<sup>2</sup></a></p> | ||
</li> | ||
<li id="user-content-user-content-fn-2"> | ||
<p>Second <a href="#user-content-fnref-2" data-footnote-backref="" aria-label="Back to reference 2" class="data-footnote-backref">↩</a></p> | ||
</li> | ||
<li id="user-content-user-content-fn-3"> | ||
<p>Last item <a href="#user-content-fnref-3" data-footnote-backref="" aria-label="Back to reference 3" class="data-footnote-backref">↩</a></p> | ||
</li> | ||
</ol> | ||
</div> | ||
</section> | ||
``` | ||
@@ -63,0 +67,0 @@ <!-- prettier-ignore-end --> |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
39606
76
2
8
12
292
1
+ Addedremark-gfm@^4.0.0
+ Added@types/debug@4.1.12(transitive)
+ Added@types/mdast@4.0.4(transitive)
+ Added@types/ms@2.1.0(transitive)
+ Added@types/unist@3.0.3(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedccount@2.0.1(transitive)
+ Addedcharacter-entities@2.0.2(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addeddecode-named-character-reference@1.0.2(transitive)
+ Addeddequal@2.0.3(transitive)
+ Addeddevlop@1.1.0(transitive)
+ Addedescape-string-regexp@5.0.0(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedis-plain-obj@4.1.0(transitive)
+ Addedlongest-streak@3.1.0(transitive)
+ Addedmarkdown-table@3.0.4(transitive)
+ Addedmdast-util-find-and-replace@3.0.2(transitive)
+ Addedmdast-util-from-markdown@2.0.2(transitive)
+ Addedmdast-util-gfm@3.0.0(transitive)
+ Addedmdast-util-gfm-autolink-literal@2.0.1(transitive)
+ Addedmdast-util-gfm-footnote@2.0.0(transitive)
+ Addedmdast-util-gfm-strikethrough@2.0.0(transitive)
+ Addedmdast-util-gfm-table@2.0.0(transitive)
+ Addedmdast-util-gfm-task-list-item@2.0.0(transitive)
+ Addedmdast-util-phrasing@4.1.0(transitive)
+ Addedmdast-util-to-markdown@2.1.2(transitive)
+ Addedmdast-util-to-string@4.0.0(transitive)
+ Addedmicromark@4.0.1(transitive)
+ Addedmicromark-core-commonmark@2.0.2(transitive)
+ Addedmicromark-extension-gfm@3.0.0(transitive)
+ Addedmicromark-extension-gfm-autolink-literal@2.1.0(transitive)
+ Addedmicromark-extension-gfm-footnote@2.1.0(transitive)
+ Addedmicromark-extension-gfm-strikethrough@2.1.0(transitive)
+ Addedmicromark-extension-gfm-table@2.1.1(transitive)
+ Addedmicromark-extension-gfm-tagfilter@2.0.0(transitive)
+ Addedmicromark-extension-gfm-task-list-item@2.1.0(transitive)
+ Addedmicromark-factory-destination@2.0.1(transitive)
+ Addedmicromark-factory-label@2.0.1(transitive)
+ Addedmicromark-factory-space@2.0.1(transitive)
+ Addedmicromark-factory-title@2.0.1(transitive)
+ Addedmicromark-factory-whitespace@2.0.1(transitive)
+ Addedmicromark-util-character@2.1.1(transitive)
+ Addedmicromark-util-chunked@2.0.1(transitive)
+ Addedmicromark-util-classify-character@2.0.1(transitive)
+ Addedmicromark-util-combine-extensions@2.0.1(transitive)
+ Addedmicromark-util-decode-numeric-character-reference@2.0.2(transitive)
+ Addedmicromark-util-decode-string@2.0.1(transitive)
+ Addedmicromark-util-encode@2.0.1(transitive)
+ Addedmicromark-util-html-tag-name@2.0.1(transitive)
+ Addedmicromark-util-normalize-identifier@2.0.1(transitive)
+ Addedmicromark-util-resolve-all@2.0.1(transitive)
+ Addedmicromark-util-sanitize-uri@2.0.1(transitive)
+ Addedmicromark-util-subtokenize@2.0.4(transitive)
+ Addedmicromark-util-symbol@2.0.1(transitive)
+ Addedmicromark-util-types@2.0.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addedremark-gfm@4.0.0(transitive)
+ Addedremark-parse@11.0.0(transitive)
+ Addedremark-stringify@11.0.0(transitive)
+ Addedtrough@2.2.0(transitive)
+ Addedunified@11.0.5(transitive)
+ Addedunist-util-is@6.0.0(transitive)
+ Addedunist-util-stringify-position@4.0.0(transitive)
+ Addedunist-util-visit@5.0.0(transitive)
+ Addedunist-util-visit-parents@6.0.1(transitive)
+ Addedvfile@6.0.3(transitive)
+ Addedvfile-message@4.0.2(transitive)
+ Addedzwitch@2.0.4(transitive)
- Removedunist-util-is@4.1.0(transitive)
Updatedunist-util-is@^6.0.0