Skip to content

Commit

Permalink
Add function to remove JS code blocks from mdx files (#17)
Browse files Browse the repository at this point in the history
* add function to remove JS code blocks from mdx files

* add `inlineCode` type to pluggin

* build
  • Loading branch information
jvc9109 authored Feb 17, 2023
1 parent 5fb7365 commit 89b4523
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
21 changes: 20 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30549,6 +30549,25 @@ var removeShortListItems = function removeShortListItems() {
});
});
};
};

var removeJsItems = function removeJsItems() {
return function (tree) {
var visitNexParagraph = false;
unistUtilVisit(tree, 'root', function (listItemNode) {
unistUtilVisit(listItemNode, ['html', 'paragraph'], function (elementNode) {
if (visitNexParagraph && elementNode.type === 'paragraph') {
unistUtilVisit(elementNode, ['text', 'inlineCode'], function (textNode) {
textNode.value = '';
});
}

if (elementNode.type === 'html') {
visitNexParagraph = elementNode.value === '<!-- JS block -->' && !visitNexParagraph;
}
});
});
};
}; // Returns scores for a given string


Expand Down Expand Up @@ -30585,7 +30604,7 @@ function averageObjectProperties(objects) {
// text we want to analyze.

function preprocessMarkdown(markdown) {
var remarker = remark().use(removeShortListItems).use(removeHeadings).use(removeAdmonitionHeadings).use(removeImageAltText).use(removeCodeBlocks).use(removePageMetadata).use(stripMarkdown);
var remarker = remark().use(removeShortListItems).use(removeHeadings).use(removeAdmonitionHeadings).use(removeImageAltText).use(removeCodeBlocks).use(removePageMetadata).use(removeJsItems).use(stripMarkdown);
return remarker.processSync(markdown).contents // Remove any blank lines
.replace(/\n+/g, "\n");
} // Calculate the readabilty result for all files found in a given path glob.
Expand Down
19 changes: 18 additions & 1 deletion src/readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ const removeShortListItems = () => (tree) => {
});
};

const removeJsItems = () => (tree) => {
let visitNexParagraph = false;
visit(tree, 'root', (listItemNode) => {
visit(listItemNode, ['html', 'paragraph'], (elementNode) => {
if (visitNexParagraph && elementNode.type === 'paragraph') {
visit(elementNode, ['text', 'inlineCode'], (textNode) => {textNode.value = ''})
}
if (elementNode.type === 'html') {
visitNexParagraph = elementNode.value === '<!-- JS block -->' && !visitNexParagraph;
}
})
})


}

// Returns scores for a given string
function scoreText(text) {
const colemanLiauIndex = readability.colemanLiauIndex(text);
Expand Down Expand Up @@ -125,6 +141,7 @@ export function preprocessMarkdown(markdown) {
.use(removeImageAltText)
.use(removeCodeBlocks)
.use(removePageMetadata)
.use(removeJsItems)
.use(strip);

return remarker
Expand All @@ -137,7 +154,7 @@ export function preprocessMarkdown(markdown) {
// This result contains readability scores for each file, and an overall average
export function calculateReadability(globPath) {
const filePaths = glob.sync(globPath);

const fileResults = filePaths.map((filePath) => {
const markdown = fs.readFileSync(filePath);
const stripped = preprocessMarkdown(markdown);
Expand Down
20 changes: 20 additions & 0 deletions src/readability.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ Test 2.`
Click the left button
Test 2.
"
`);
});

it('should strip js code parts from comment block', () => {
const stripped = preprocessMarkdown(`
<!-- JS block -->
import MenuGrid from '../../components/MenuGrid';
const items = [
{title: 'Cashier strategies', to: '/docs/settings/cashier-strategies/' },
{title: 'Custom cashier properties', to: '/docs/settings/custom-cashier-property-sets/' },
];
<!-- JS block -->
Some content. This is paragraph with const items = [];
`);

expect(stripped).toMatchInlineSnapshot(`
"
Some content. This is paragraph with const items = \\\\[];
"
`);
});
});

0 comments on commit 89b4523

Please sign in to comment.