mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-08 12:57:45 +00:00
Merge branch 'workaround-gatsby-node' into headings-with-id-plugin
# Conflicts: # package-lock.json # package.json # plugins/count-inline-code/gatsby-node.js
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
// I would rather have these in their respective plugins folders, but Gatsby
|
||||
// didn't like having MarkdownRemarkFields implemented twice
|
||||
/**
|
||||
* While I would much MUCH rather utilize the existing AST manipulation from
|
||||
* the remarked plugin, we've hit a bit of a snag. The problem is explained here:
|
||||
* https://github.com/gatsbyjs/gatsby/issues/22287
|
||||
*
|
||||
* Once this issue is resolved/workedaround, we can move back to the code that
|
||||
* was previously confirmed working here:
|
||||
* https://github.com/unicorn-utterances/unicorn-utterances/tree/c6d64a44ee8a4e7d6cad1dbd2d01bc9a6ad78241/plugins/count-inline-code
|
||||
*/
|
||||
const flatFilter = require("unist-util-flat-filter");
|
||||
const parse = require("@textlint/markdown-to-ast").parse;
|
||||
|
||||
exports.createSchemaCustomization = ({ actions }) => {
|
||||
const { createTypes } = actions;
|
||||
const typeDefs = `
|
||||
@@ -15,3 +25,28 @@ exports.createSchemaCustomization = ({ actions }) => {
|
||||
`;
|
||||
createTypes(typeDefs);
|
||||
};
|
||||
exports.sourceNodes = ({ getNodesByType, actions }) => {
|
||||
const postNodes = getNodesByType(`MarkdownRemark`);
|
||||
const { createNodeField } = actions;
|
||||
postNodes.forEach(postNode => {
|
||||
const markdownAST = parse(postNode.rawMarkdownBody);
|
||||
const inlineCodeAST = flatFilter(markdownAST, node => node.type === "Code");
|
||||
let inlineWords = 0;
|
||||
if (inlineCodeAST && inlineCodeAST.children) {
|
||||
inlineWords = inlineCodeAST.children
|
||||
// Prevent grabbing from https://github.com/nullhook/gatsby-remark-video
|
||||
.filter(child => !child.value.startsWith("video:"))
|
||||
.reduce((numberOfInline, inlineCodeNode) => {
|
||||
const { value } = inlineCodeNode;
|
||||
const words = value.split(/\b/g);
|
||||
return numberOfInline + words.length;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
createNodeField({
|
||||
name: `inlineCount`,
|
||||
node: postNode,
|
||||
value: inlineWords
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user