mirror of
https://github.com/kogakure/website-astro-stefanimhoff.de.git
synced 2026-02-03 12:05:28 +00:00
feat: add remark widont extension
Idea from this article: https://eatmon.co/blog/remove-runts-markdown/
This commit is contained in:
@@ -2,7 +2,7 @@ import mdx from '@astrojs/mdx';
|
||||
import preact from '@astrojs/preact';
|
||||
import tailwind from '@astrojs/tailwind';
|
||||
import { astroImageTools } from 'astro-imagetools';
|
||||
import { remarkReadingTime } from './src/utils';
|
||||
import { remarkReadingTime, remarkWidont } from './src/utils';
|
||||
|
||||
import { defineConfig } from 'astro/config';
|
||||
|
||||
@@ -17,7 +17,7 @@ export default defineConfig({
|
||||
},
|
||||
integrations: [
|
||||
mdx({
|
||||
remarkPlugins: [remarkReadingTime],
|
||||
remarkPlugins: [remarkReadingTime, remarkWidont],
|
||||
}),
|
||||
tailwind(),
|
||||
preact({ compat: true }),
|
||||
|
||||
@@ -40,7 +40,8 @@
|
||||
"react-dom": "npm:@preact/compat@^17.1.2",
|
||||
"reading-time": "^1.5.0",
|
||||
"sharp": "^0.32.1",
|
||||
"tailwindcss": "^3.3.2"
|
||||
"tailwindcss": "^3.3.2",
|
||||
"unist-util-visit": "^4.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mdast": "^3.0.11",
|
||||
|
||||
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@@ -84,6 +84,9 @@ dependencies:
|
||||
tailwindcss:
|
||||
specifier: ^3.3.2
|
||||
version: 3.3.2
|
||||
unist-util-visit:
|
||||
specifier: ^4.1.2
|
||||
version: 4.1.2
|
||||
|
||||
devDependencies:
|
||||
'@types/mdast':
|
||||
|
||||
@@ -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