Skip to content
Merged
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
14 changes: 13 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

46 changes: 45 additions & 1 deletion src/readability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,51 @@ This is the only content.
);

expect(stripped).toMatchInlineSnapshot(`
"This is the only content.
"
This is the only content.
"
`);
});

it('should remove horizontal rules', () => {
const stripped = preprocessMarkdown(
`
Example text 1.

---

Example text 2.

`,
);

expect(stripped).toMatchInlineSnapshot(`
"Example text 1.
Example text 2.
"
`);
});

it('should remove horizontal rules with front matter', () => {
const stripped = preprocessMarkdown(
`---
title: "Front matter"
nested:
description: "Cashier strategies"
---

Example text 1.

---

Example text 2.

`,
);

expect(stripped).toMatchInlineSnapshot(`
"Example text 1.
Example text 2.
"
`);
});
Expand Down
15 changes: 14 additions & 1 deletion src/readability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,19 @@ const removeFrontmatter: Plugin = () => (tree) => {
} else {
// Remove the two thematic breaks and all children
// @ts-ignore
node?.children.splice(0, secondThematicBreakIndex + 1);
node?.children.splice(0, secondThematicBreakIndex - 1);
}
});
};

// Remove all horizontal rules from the Markdown.
// Horizontal lines are not a part of the sentence structure,
// so we should remove them.
const removeHorizontalRules: Plugin = () => (tree) => {
visit(tree, 'thematicBreak', (_, index, parent) => {
// Remove the thematicBreak node from its parent's children array
if (parent) {
parent.children.splice(index, 1);
}
});
};
Expand Down Expand Up @@ -376,6 +388,7 @@ export function preprocessMarkdown(markdown: string) {
.use(removeUnwantedNodeTypes)
.use(removeImageAltText)
.use(removeFrontmatter)
.use(removeHorizontalRules)
.use(replaceNodesWithTheirTextContent);

return (
Expand Down