Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions __tests__/stripComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { stripComments } from '../index';

describe('Strip Comments', () => {
it('removes HTML comments from the text', () => {
const input = `
# hello world
This is some text.
<!-- This is a comment that should be removed -->
More text here.
<!-- Another comment -->`;

const expectedOutput = `
This is some text.
More text here.`;

expect(stripComments(input)).toBe(expectedOutput);
});
});
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const toPlainText = require('./processor/plugin/plain-text');
const sectionAnchorId = require('./processor/plugin/section-anchor-id');
const tableFlattening = require('./processor/plugin/table-flattening');
const { remarkTransformers, rehypeTransformers } = require('./processor/transform');
const stripCommentsTransformer = require('./processor/transform/strip-comments');
const createSchema = require('./sanitize.schema');

const {
Expand Down Expand Up @@ -107,7 +108,6 @@ export function processor(userOpts = {}) {
const [, parsedOpts] = setup('', userOpts);
const { sanitize } = parsedOpts;
const [reusableContent, opts] = parseReusableContent(parsedOpts);

return unified()
.use(remarkParse, opts.markdownOptions)
.use(remarkFrontmatter, ['yaml', 'toml'])
Expand Down Expand Up @@ -303,6 +303,16 @@ export function md(treeOrString, opts = {}) {
return processor(opts).use(remarkStringify, opts.markdownOptions).use(customCompilers).stringify(tree);
}

/**
* Strip HTML comments from markdown text
*/
export function stripComments(text, opts = {}) {
if (!text) return null;
[text, opts] = setup(text, opts);

return processor(opts).use(stripCommentsTransformer).use(remarkStringify).processSync(text).toString();
}

const ReadMeMarkdown = (text, opts = {}) => react(text, opts);

export { default as Owlmoji } from './lib/owlmoji';
Expand Down
25 changes: 25 additions & 0 deletions processor/transform/strip-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { SKIP, visit } from 'unist-util-visit';

const HTML_COMMENT_REGEX = /<!--[\s\S]*?-->/g;

const stripCommentsTransformer = () => tree => {
console.log('STRIP COMMENTS TRANSFORMER', tree);

Check warning on line 6 in processor/transform/strip-comments.js

View workflow job for this annotation

GitHub Actions / Test (20, 18)

Unexpected console statement

Check warning on line 6 in processor/transform/strip-comments.js

View workflow job for this annotation

GitHub Actions / Test (20, 16)

Unexpected console statement

visit(tree, (node, index, parent) => {
// Remove HTML comments
if (node.type === 'html' && HTML_COMMENT_REGEX.test(node.value)) {
const newValue = node.value.replace(HTML_COMMENT_REGEX, '').trim();
if (newValue) {
node.value = newValue;
} else {
parent.children.splice(index, 1);
return [SKIP, index];
}
}
return node;
});

return tree;
};

export default stripCommentsTransformer;
Loading