rehype-postfix-footnote-anchors
Advanced tools
Comparing version 1.0.8 to 2.0.0
@@ -5,2 +5,35 @@ "use strict"; | ||
function findLastTag(node) { | ||
var tag = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'p'; | ||
if (!node.children || !node.children.length) return; | ||
var links = node.children.filter(function (e) { | ||
return e.tagName === tag; | ||
}); | ||
if (!links.length) return; | ||
return links[links.length - 1]; | ||
} | ||
function findLastLink(node, className) { | ||
if (!node.children || !node.children.length) return; | ||
var links = node.children.filter(function (e) { | ||
return e.tagName === 'a'; | ||
}); | ||
if (!links.length) return; | ||
var aTag = links[links.length - 1]; | ||
if (!aTag.properties || !aTag.properties.className || !aTag.properties.className.includes(className)) return; | ||
return aTag; | ||
} | ||
function setPostfix(node, aTag, postfix) { | ||
if (typeof postfix === 'function') { | ||
var id = node.properties.id; | ||
node.properties.id = postfix(id); | ||
var link = aTag.properties.href; | ||
aTag.properties.href = "#".concat(postfix(link.substr(1))); | ||
} else { | ||
node.properties.id += postfix; | ||
aTag.properties.href += postfix; | ||
} | ||
} | ||
function plugin() { | ||
@@ -15,13 +48,12 @@ var postfix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '-postfix'; | ||
if (!node.properties.id.startsWith('fn-')) return; | ||
if (!node.children.length || node.children[node.children.length - 2].tagName !== 'a') return; | ||
if (!node.children.length) return; | ||
var aTag = findLastLink(node, 'footnote-backref'); | ||
if (typeof postfix === 'function') { | ||
var id = node.properties.id; | ||
node.properties.id = postfix(id); | ||
var link = node.children[node.children.length - 2].properties.href; | ||
node.children[node.children.length - 2].properties.href = "#".concat(postfix(link.substr(1))); | ||
} else { | ||
node.properties.id += postfix; | ||
node.children[node.children.length - 2].properties.href += postfix; | ||
if (!aTag) { | ||
var pTag = findLastTag(node, 'p'); | ||
aTag = findLastLink(pTag, 'footnote-backref'); | ||
} | ||
if (!aTag) return; | ||
setPostfix(node, aTag, postfix); | ||
} | ||
@@ -33,12 +65,5 @@ | ||
if (!node.children.length || node.children[0].tagName !== 'a') return; | ||
if (typeof postfix === 'function') { | ||
var _id = node.properties.id; | ||
node.properties.id = postfix(_id); | ||
var _link = node.children[0].properties.href; | ||
node.children[0].properties.href = "#".concat(postfix(_link.substr(1))); | ||
} else { | ||
node.properties.id += postfix; | ||
node.children[0].properties.href += postfix; | ||
} | ||
var _aTag = node.children[0]; | ||
if (!_aTag.properties || !_aTag.properties.className || !_aTag.properties.className.includes('footnote-ref')) return; | ||
setPostfix(node, _aTag, postfix); | ||
} | ||
@@ -45,0 +70,0 @@ }); |
{ | ||
"name": "rehype-postfix-footnote-anchors", | ||
"version": "1.0.8", | ||
"version": "2.0.0", | ||
"repository": { | ||
@@ -36,8 +36,8 @@ "url": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/rehype-postfix-footnote-anchors", | ||
"dependencies": { | ||
"unist-util-visit": "^1.3.1" | ||
"unist-util-visit": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"remark-numbered-footnotes": "^1.1.0" | ||
"remark-numbered-footnotes": "^2.0.0" | ||
}, | ||
"gitHead": "b05f25a9f9f7be3c2987fb3c07f48cc1b43ca2aa" | ||
"gitHead": "c2bf169996c4adfdf75a55290080947a2384c09f" | ||
} |
const visit = require('unist-util-visit') | ||
function findLastTag (node, tag = 'p') { | ||
if (!node.children || !node.children.length) return | ||
const links = node.children.filter(e => e.tagName === tag) | ||
if (!links.length) return | ||
return links[links.length - 1] | ||
} | ||
function findLastLink (node, className) { | ||
if (!node.children || !node.children.length) return | ||
const links = node.children.filter(e => e.tagName === 'a') | ||
if (!links.length) return | ||
const aTag = links[links.length - 1] | ||
if (!aTag.properties || !aTag.properties.className || | ||
!aTag.properties.className.includes(className)) return | ||
return aTag | ||
} | ||
function setPostfix (node, aTag, postfix) { | ||
if (typeof postfix === 'function') { | ||
const id = node.properties.id | ||
node.properties.id = postfix(id) | ||
const link = aTag.properties.href | ||
aTag.properties.href = `#${postfix(link.substr(1))}` | ||
} else { | ||
node.properties.id += postfix | ||
aTag.properties.href += postfix | ||
} | ||
} | ||
function plugin (postfix = '-postfix') { | ||
@@ -10,13 +37,13 @@ return (tree) => { | ||
if (!node.properties.id.startsWith('fn-')) return | ||
if (!node.children.length || node.children[node.children.length - 2].tagName !== 'a') return | ||
if (typeof postfix === 'function') { | ||
const id = node.properties.id | ||
node.properties.id = postfix(id) | ||
const link = node.children[node.children.length - 2].properties.href | ||
node.children[node.children.length - 2].properties.href = `#${postfix(link.substr(1))}` | ||
} else { | ||
node.properties.id += postfix | ||
node.children[node.children.length - 2].properties.href += postfix | ||
if (!node.children.length) return | ||
let aTag = findLastLink(node, 'footnote-backref') | ||
if (!aTag) { | ||
const pTag = findLastTag(node, 'p') | ||
aTag = findLastLink(pTag, 'footnote-backref') | ||
} | ||
if (!aTag) return | ||
setPostfix(node, aTag, postfix) | ||
} | ||
@@ -29,11 +56,8 @@ | ||
if (typeof postfix === 'function') { | ||
const id = node.properties.id | ||
node.properties.id = postfix(id) | ||
const link = node.children[0].properties.href | ||
node.children[0].properties.href = `#${postfix(link.substr(1))}` | ||
} else { | ||
node.properties.id += postfix | ||
node.children[0].properties.href += postfix | ||
} | ||
const aTag = node.children[0] | ||
if (!aTag.properties || !aTag.properties.className || | ||
!aTag.properties.className.includes('footnote-ref')) return | ||
setPostfix(node, aTag, postfix) | ||
} | ||
@@ -40,0 +64,0 @@ }) |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
9050
116
1
+ Added@types/unist@2.0.11(transitive)
+ Addedunist-util-is@4.1.0(transitive)
+ Addedunist-util-visit@2.0.3(transitive)
+ Addedunist-util-visit-parents@3.1.1(transitive)
- Removedunist-util-is@3.0.0(transitive)
- Removedunist-util-visit@1.4.1(transitive)
- Removedunist-util-visit-parents@2.1.2(transitive)
Updatedunist-util-visit@^2.0.1