From f325cebda053cae80b60fd2b208284d517f0c8b9 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 22 Jun 2023 21:59:47 +0400 Subject: [PATCH] Custom mdast util --- package-lock.json | 41 +++++++++++++++++++++++++++++++++++++++-- package.json | 5 +++-- remark-post-meta.mjs | 32 +++++++++++++++++++++----------- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 584a613..0b49156 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 9f7e600..d6eab13 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/remark-post-meta.mjs b/remark-post-meta.mjs index b9d83ab..4d89c5d 100644 --- a/remark-post-meta.mjs +++ b/remark-post-meta.mjs @@ -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() }