mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-10 21:07:46 +00:00
Got counting working
This commit is contained in:
@@ -1,3 +1,15 @@
|
||||
/**
|
||||
* 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 = `
|
||||
@@ -7,3 +19,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
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
const flatFilter = require("unist-util-flat-filter");
|
||||
|
||||
const addInlineCount = ({ markdownAST, actions, markdownNode, ...props }) => {
|
||||
const { createNodeField } = actions;
|
||||
const inlineCodeAST = flatFilter(
|
||||
markdownAST,
|
||||
node => node.type === "inlineCode"
|
||||
);
|
||||
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`,
|
||||
node: markdownNode,
|
||||
value: inlineWords
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = addInlineCount;
|
||||
23
plugins/count-inline-code/package-lock.json
generated
23
plugins/count-inline-code/package-lock.json
generated
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"name": "count-inline-code",
|
||||
"version": "0.0.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"unist-util-flat-filter": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/unist-util-flat-filter/-/unist-util-flat-filter-1.0.0.tgz",
|
||||
"integrity": "sha512-zjTmbMgyL1V7iUCnFaGLms9HtGbqK4d3YINqzu+wiMbau93GdiDGtJiB8lMhAyxAaxIAK2rOCeBCldF0FUreKQ==",
|
||||
"requires": {
|
||||
"unist-util-is": "^4.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"unist-util-is": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-4.0.1.tgz",
|
||||
"integrity": "sha512-7NYjErP4LJtkEptPR22wO5RsCPnHZZrop7t2SoQzjvpFedCFer4WW8ujj9GI5DkUX7yVcffXLjoURf6h2QUv6Q=="
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "count-inline-code",
|
||||
"name": "count-inline-code-2",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
|
||||
Reference in New Issue
Block a user