mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-10 12:57:46 +00:00
Add initial count-inline-code plugin
This commit is contained in:
37
plugins/count-inline-code/index.js
Normal file
37
plugins/count-inline-code/index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const flatFilter = (ast, truthyFunc) => {
|
||||
if (!ast) return [];
|
||||
if (truthyFunc(ast)) return [ast];
|
||||
if (!ast.children) return [];
|
||||
let acceptedChildren = [];
|
||||
for (const child of ast.children) {
|
||||
const flatFilterResult = flatFilter(child, truthyFunc);
|
||||
if (flatFilterResult && flatFilterResult.length) {
|
||||
// Take array results and push to the returned array to flatten it
|
||||
flatFilterResult.forEach(flatFilterResultItem => {
|
||||
acceptedChildren.push(flatFilterResultItem);
|
||||
});
|
||||
}
|
||||
}
|
||||
return acceptedChildren;
|
||||
};
|
||||
|
||||
const addVideo = ({ markdownAST, actions, markdownNode, ...props }) => {
|
||||
const { createNodeField } = actions;
|
||||
const inlineCodeAST = flatFilter(
|
||||
markdownAST,
|
||||
node => node.type === "inlineCode"
|
||||
);
|
||||
const inlineWords = inlineCodeAST.reduce((numberOfInline, inlineCodeNode) => {
|
||||
const { value } = inlineCodeNode;
|
||||
const words = value.split(/\b/g);
|
||||
return numberOfInline + words.length;
|
||||
}, 0);
|
||||
|
||||
createNodeField({
|
||||
name: `inlineCount`,
|
||||
node: markdownNode,
|
||||
value: inlineWords
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = addVideo;
|
||||
Reference in New Issue
Block a user