Custom mdast util

This commit is contained in:
DarkCat09 2023-06-22 21:59:47 +04:00
parent 3d07dddef4
commit f325cebda0
3 changed files with 63 additions and 15 deletions

41
package-lock.json generated
View file

@ -10,12 +10,13 @@
"astro": "^2.3.0", "astro": "^2.3.0",
"astro-compress": "^1.1.42", "astro-compress": "^1.1.42",
"less": "^4.1.3", "less": "^4.1.3",
"mdast-util-to-string": "^3.2.0",
"reading-time": "^1.5.0", "reading-time": "^1.5.0",
"rehype-autolink-headings": "^6.1.1", "rehype-autolink-headings": "^6.1.1",
"rehype-figure": "^1.0.1", "rehype-figure": "^1.0.1",
"rehype-slug": "^5.1.0", "rehype-slug": "^5.1.0",
"remark-unwrap-images": "^3.0.1" "remark-unwrap-images": "^3.0.1",
"unist-util-select": "^4.0.3",
"unist-util-visit": "^4.1.2"
} }
}, },
"node_modules/@ampproject/remapping": { "node_modules/@ampproject/remapping": {
@ -2330,6 +2331,11 @@
"url": "https://github.com/sponsors/fb55" "url": "https://github.com/sponsors/fb55"
} }
}, },
"node_modules/css-selector-parser": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/css-selector-parser/-/css-selector-parser-1.4.1.tgz",
"integrity": "sha512-HYPSb7y/Z7BNDCOrakL4raGO2zltZkbeXyAd6Tg9obzix6QhzxCotdBl6VT0Dv4vZfJGVz3WL/xaEI9Ly3ul0g=="
},
"node_modules/css-tree": { "node_modules/css-tree": {
"version": "2.2.1", "version": "2.2.1",
"resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz", "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz",
@ -6404,6 +6410,21 @@
"url": "https://opencollective.com/unified" "url": "https://opencollective.com/unified"
} }
}, },
"node_modules/unist-util-select": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/unist-util-select/-/unist-util-select-4.0.3.tgz",
"integrity": "sha512-1074+K9VyR3NyUz3lgNtHKm7ln+jSZXtLJM4E22uVuoFn88a/Go2pX8dusrt/W+KWH1ncn8jcd8uCQuvXb/fXA==",
"dependencies": {
"@types/unist": "^2.0.0",
"css-selector-parser": "^1.0.0",
"nth-check": "^2.0.0",
"zwitch": "^2.0.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
}
},
"node_modules/unist-util-stringify-position": { "node_modules/unist-util-stringify-position": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz", "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz",
@ -8347,6 +8368,11 @@
"nth-check": "^2.0.1" "nth-check": "^2.0.1"
} }
}, },
"css-selector-parser": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/css-selector-parser/-/css-selector-parser-1.4.1.tgz",
"integrity": "sha512-HYPSb7y/Z7BNDCOrakL4raGO2zltZkbeXyAd6Tg9obzix6QhzxCotdBl6VT0Dv4vZfJGVz3WL/xaEI9Ly3ul0g=="
},
"css-tree": { "css-tree": {
"version": "2.2.1", "version": "2.2.1",
"resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz", "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz",
@ -11073,6 +11099,17 @@
"@types/unist": "^2.0.0" "@types/unist": "^2.0.0"
} }
}, },
"unist-util-select": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/unist-util-select/-/unist-util-select-4.0.3.tgz",
"integrity": "sha512-1074+K9VyR3NyUz3lgNtHKm7ln+jSZXtLJM4E22uVuoFn88a/Go2pX8dusrt/W+KWH1ncn8jcd8uCQuvXb/fXA==",
"requires": {
"@types/unist": "^2.0.0",
"css-selector-parser": "^1.0.0",
"nth-check": "^2.0.0",
"zwitch": "^2.0.0"
}
},
"unist-util-stringify-position": { "unist-util-stringify-position": {
"version": "3.0.3", "version": "3.0.3",
"resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz", "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz",

View file

@ -13,11 +13,12 @@
"astro": "^2.3.0", "astro": "^2.3.0",
"astro-compress": "^1.1.42", "astro-compress": "^1.1.42",
"less": "^4.1.3", "less": "^4.1.3",
"mdast-util-to-string": "^3.2.0",
"reading-time": "^1.5.0", "reading-time": "^1.5.0",
"rehype-autolink-headings": "^6.1.1", "rehype-autolink-headings": "^6.1.1",
"rehype-figure": "^1.0.1", "rehype-figure": "^1.0.1",
"rehype-slug": "^5.1.0", "rehype-slug": "^5.1.0",
"remark-unwrap-images": "^3.0.1" "remark-unwrap-images": "^3.0.1",
"unist-util-select": "^4.0.3",
"unist-util-visit": "^4.1.2"
} }
} }

View file

@ -1,16 +1,26 @@
import getReadingTime from 'reading-time'; import { select } from 'unist-util-select'
import { toString as mdToString } from 'mdast-util-to-string'; import { visit } from 'unist-util-visit'
import getReadingTime from 'reading-time'
const ELLIPSIS = '\u2026'; export default function remarkPostMeta() {
return (tree, { data }) => {
const firstPara = select(':not(heading) paragraph:first-of-type', tree)
export default function() { const firstParaText = mdToString(firstPara)
return function (tree, { data }) { const articleText = mdToString(tree)
const textOnPage = mdToString(tree);
const readingTime = getReadingTime(textOnPage); const readingTime = getReadingTime(articleText)
data.astro.frontmatter.readingTime = readingTime.text; data.astro.frontmatter.readingTime = readingTime.minutes
const description = textOnPage.slice(0, 100) + ELLIPSIS; data.astro.frontmatter.description = firstParaText
data.astro.frontmatter.description = description; }
}; }
function mdToString(tree) {
let str = ''
visit(tree, 'text', node => {
str += node.value
str += ' '
})
return str.trimEnd()
} }