Migrate to parent unist package

This commit is contained in:
Corbin Crutchley
2020-01-15 11:48:29 -08:00
parent 8ef20c4ca2
commit e3e81ecbdf
6 changed files with 45 additions and 66 deletions

View File

@@ -1,31 +1,22 @@
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 flatFilter = require("unist-util-flat-filter");
const addVideo = ({ markdownAST, actions, markdownNode, ...props }) => {
const addInlineCount = ({ 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);
let inlineWords = 0;
if (inlineCodeAST && inlineCodeAST.children) {
inlineWords = inlineCodeAST.children.reduce(
(numberOfInline, inlineCodeNode) => {
const { value } = inlineCodeNode;
const words = value.split(/\b/g);
return numberOfInline + words.length;
},
0
);
}
createNodeField({
name: `inlineCount`,
@@ -34,4 +25,4 @@ const addVideo = ({ markdownAST, actions, markdownNode, ...props }) => {
});
};
module.exports = addVideo;
module.exports = addInlineCount;