Custom mdast util
This commit is contained in:
parent
3d07dddef4
commit
f325cebda0
3 changed files with 63 additions and 15 deletions
41
package-lock.json
generated
41
package-lock.json
generated
|
@ -10,12 +10,13 @@
|
|||
"astro": "^2.3.0",
|
||||
"astro-compress": "^1.1.42",
|
||||
"less": "^4.1.3",
|
||||
"mdast-util-to-string": "^3.2.0",
|
||||
"reading-time": "^1.5.0",
|
||||
"rehype-autolink-headings": "^6.1.1",
|
||||
"rehype-figure": "^1.0.1",
|
||||
"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": {
|
||||
|
@ -2330,6 +2331,11 @@
|
|||
"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": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz",
|
||||
|
@ -6404,6 +6410,21 @@
|
|||
"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": {
|
||||
"version": "3.0.3",
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"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": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz",
|
||||
|
@ -11073,6 +11099,17 @@
|
|||
"@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": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz",
|
||||
|
|
|
@ -13,11 +13,12 @@
|
|||
"astro": "^2.3.0",
|
||||
"astro-compress": "^1.1.42",
|
||||
"less": "^4.1.3",
|
||||
"mdast-util-to-string": "^3.2.0",
|
||||
"reading-time": "^1.5.0",
|
||||
"rehype-autolink-headings": "^6.1.1",
|
||||
"rehype-figure": "^1.0.1",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,26 @@
|
|||
import getReadingTime from 'reading-time';
|
||||
import { toString as mdToString } from 'mdast-util-to-string';
|
||||
import { select } from 'unist-util-select'
|
||||
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() {
|
||||
return function (tree, { data }) {
|
||||
const textOnPage = mdToString(tree);
|
||||
const firstParaText = mdToString(firstPara)
|
||||
const articleText = mdToString(tree)
|
||||
|
||||
const readingTime = getReadingTime(textOnPage);
|
||||
data.astro.frontmatter.readingTime = readingTime.text;
|
||||
const readingTime = getReadingTime(articleText)
|
||||
data.astro.frontmatter.readingTime = readingTime.minutes
|
||||
|
||||
const description = textOnPage.slice(0, 100) + ELLIPSIS;
|
||||
data.astro.frontmatter.description = description;
|
||||
};
|
||||
data.astro.frontmatter.description = firstParaText
|
||||
}
|
||||
}
|
||||
|
||||
function mdToString(tree) {
|
||||
let str = ''
|
||||
visit(tree, 'text', node => {
|
||||
str += node.value
|
||||
str += ' '
|
||||
})
|
||||
return str.trimEnd()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue