mirror of
https://github.com/kogakure/website-astro-stefanimhoff.de.git
synced 2026-02-03 20:15:27 +00:00
feat: add remark widont extension
Idea from this article: https://eatmon.co/blog/remove-runts-markdown/
This commit is contained in:
@@ -2,6 +2,7 @@ export * from './date';
|
||||
export * from './format-posts';
|
||||
export * from './pick-two-random-colors';
|
||||
export * from './remark-reading-time';
|
||||
export * from './remark-widont';
|
||||
export * from './sort-by-alphabet';
|
||||
export * from './sort-by-date';
|
||||
export * from './sort-by-sortkey';
|
||||
|
||||
17
src/utils/remark-widont.ts
Normal file
17
src/utils/remark-widont.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { visit } from 'unist-util-visit';
|
||||
|
||||
import type { Literal, Node } from 'unist';
|
||||
|
||||
export function remarkWidont() {
|
||||
function transformer(tree: Node) {
|
||||
visit(tree, 'text', function (node: Literal<string>) {
|
||||
let wordCount = node.value.split(' ').length;
|
||||
|
||||
if (wordCount >= 3) {
|
||||
node.value = node.value.replace(/ ([^ ]*)$/, '\u00A0$1');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return transformer;
|
||||
}
|
||||
Reference in New Issue
Block a user