From 5d77abf77cc38c430b3b36dc742f43a6b0d993de Mon Sep 17 00:00:00 2001 From: radishzzz Date: Tue, 14 Jan 2025 01:06:09 +0000 Subject: [PATCH] feat: markdown extended features from fuwari --- astro.config.ts | 47 ++++++++++++++++++------------ src/plugins/remark-excerpt.ts | 25 +++++++--------- src/plugins/remark-reading-time.ts | 19 +++++++++++- 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/astro.config.ts b/astro.config.ts index fb767e1..b696299 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -7,14 +7,14 @@ import robotsTxt from 'astro-robots-txt' import { defineConfig } from 'astro/config' // Rehype import rehypeAutolinkHeadings from 'rehype-autolink-headings' +import rehypeComponents from 'rehype-components' import rehypeExternalLinks from 'rehype-external-links' import rehypeKatex from 'rehype-katex' import rehypePrettyCode from 'rehype-pretty-code' import rehypeSlug from 'rehype-slug' // Remark import remarkDirective from 'remark-directive' -import remarkDirectiveRehype from 'remark-directive-rehype' -import remarkGithubAdmonitions from 'remark-github-admonitions-to-directives' +import remarkGithubAdmonitionsToDirectives from 'remark-github-admonitions-to-directives' import remarkMath from 'remark-math' import remarkSectionize from 'remark-sectionize' // @@ -24,6 +24,7 @@ import { AdmonitionComponent } from './src/plugins/rehype-component-admonition.t import { GithubCardComponent } from './src/plugins/rehype-component-github-card.ts' import { parseDirectiveNode } from './src/plugins/remark-directive-rehype.ts' import { remarkExcerpt } from './src/plugins/remark-excerpt.ts' + import { remarkReadingTime } from './src/plugins/remark-reading-time.ts' const { url }: { url: ThemeConfig['site']['url'] } = themeConfig.site @@ -54,26 +55,37 @@ export default defineConfig({ remarkMath, remarkReadingTime, remarkExcerpt, - remarkGithubAdmonitions, + remarkGithubAdmonitionsToDirectives, remarkDirective, - remarkDirectiveRehype, remarkSectionize, parseDirectiveNode, ], rehypePlugins: [ rehypeKatex, rehypeSlug, - rehypePrettyCode, + [ + rehypePrettyCode, + { + theme: 'github-dark', + }, + ], + [ + rehypeExternalLinks, + { + target: '_blank', + rel: ['nofollow', 'noopener', 'noreferrer'], + }, + ], [ rehypeComponents, { components: { github: GithubCardComponent, - note: (x: any, y: any) => AdmonitionComponent(x, y, 'note'), - tip: (x: any, y: any) => AdmonitionComponent(x, y, 'tip'), - important: (x: any, y: any) => AdmonitionComponent(x, y, 'important'), - caution: (x: any, y: any) => AdmonitionComponent(x, y, 'caution'), - warning: (x: any, y: any) => AdmonitionComponent(x, y, 'warning'), + note: (props, children) => AdmonitionComponent(props, 'note', children), + tip: (props, children) => AdmonitionComponent(props, 'tip', children), + important: (props, children) => AdmonitionComponent(props, 'important', children), + caution: (props, children) => AdmonitionComponent(props, 'caution', children), + warning: (props, children) => AdmonitionComponent(props, 'warning', children), }, }, ], @@ -91,17 +103,16 @@ export default defineConfig({ 'className': ['anchor-icon'], 'data-pagefind-ignore': true, }, - children: [{ type: 'text', value: '#' }], + children: [ + { + type: 'text', + value: '#', + }, + ], }, }, ], - [ - rehypeExternalLinks, - { - target: '_blank', - rel: ['nofollow', 'noopener', 'noreferrer'], - }, - ], ], }, + }) diff --git a/src/plugins/remark-excerpt.ts b/src/plugins/remark-excerpt.ts index a2e3e90..69e4b72 100644 --- a/src/plugins/remark-excerpt.ts +++ b/src/plugins/remark-excerpt.ts @@ -1,26 +1,21 @@ import type { Root } from 'mdast' +import type { VFile } from 'vfile' import { toString } from 'mdast-util-to-string' -interface AstroData { - data: { - astro: { - frontmatter: { - excerpt: string - } - } - } -} - export function remarkExcerpt() { - return (tree: Root, file: AstroData) => { + return function (tree: Root, file: VFile) { let excerpt = '' for (const node of tree.children) { - if (node.type !== 'paragraph') { - continue + if (node.type === 'paragraph') { + excerpt = toString(node) + break } - excerpt = toString(node) - break } + + // 确保 data.astro.frontmatter 存在 + file.data.astro = file.data.astro || {} + file.data.astro.frontmatter = file.data.astro.frontmatter || {} + file.data.astro.frontmatter.excerpt = excerpt } } diff --git a/src/plugins/remark-reading-time.ts b/src/plugins/remark-reading-time.ts index 0519ecb..8e6fee1 100644 --- a/src/plugins/remark-reading-time.ts +++ b/src/plugins/remark-reading-time.ts @@ -1 +1,18 @@ - \ No newline at end of file +import type { Root } from 'mdast' +import type { VFile } from 'vfile' +import { toString } from 'mdast-util-to-string' +import getReadingTime from 'reading-time' + +export function remarkReadingTime() { + return function (tree: Root, file: VFile) { + const textOnPage = toString(tree) + const readingTime = getReadingTime(textOnPage) + + // 确保 data.astro.frontmatter 存在 + file.data.astro = file.data.astro || {} + file.data.astro.frontmatter = file.data.astro.frontmatter || {} + + file.data.astro.frontmatter.minutes = Math.max(1, Math.round(readingTime.minutes)) + file.data.astro.frontmatter.words = readingTime.words + } +}