mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 11:41:17 +02:00
feat: markdown extended features from fuwari
This commit is contained in:
parent
9e9cdcb206
commit
5d77abf77c
3 changed files with 57 additions and 34 deletions
|
@ -7,14 +7,14 @@ import robotsTxt from 'astro-robots-txt'
|
||||||
import { defineConfig } from 'astro/config'
|
import { defineConfig } from 'astro/config'
|
||||||
// Rehype
|
// Rehype
|
||||||
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
|
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
|
||||||
|
import rehypeComponents from 'rehype-components'
|
||||||
import rehypeExternalLinks from 'rehype-external-links'
|
import rehypeExternalLinks from 'rehype-external-links'
|
||||||
import rehypeKatex from 'rehype-katex'
|
import rehypeKatex from 'rehype-katex'
|
||||||
import rehypePrettyCode from 'rehype-pretty-code'
|
import rehypePrettyCode from 'rehype-pretty-code'
|
||||||
import rehypeSlug from 'rehype-slug'
|
import rehypeSlug from 'rehype-slug'
|
||||||
// Remark
|
// Remark
|
||||||
import remarkDirective from 'remark-directive'
|
import remarkDirective from 'remark-directive'
|
||||||
import remarkDirectiveRehype from 'remark-directive-rehype'
|
import remarkGithubAdmonitionsToDirectives from 'remark-github-admonitions-to-directives'
|
||||||
import remarkGithubAdmonitions from 'remark-github-admonitions-to-directives'
|
|
||||||
import remarkMath from 'remark-math'
|
import remarkMath from 'remark-math'
|
||||||
import remarkSectionize from 'remark-sectionize'
|
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 { GithubCardComponent } from './src/plugins/rehype-component-github-card.ts'
|
||||||
import { parseDirectiveNode } from './src/plugins/remark-directive-rehype.ts'
|
import { parseDirectiveNode } from './src/plugins/remark-directive-rehype.ts'
|
||||||
import { remarkExcerpt } from './src/plugins/remark-excerpt.ts'
|
import { remarkExcerpt } from './src/plugins/remark-excerpt.ts'
|
||||||
|
|
||||||
import { remarkReadingTime } from './src/plugins/remark-reading-time.ts'
|
import { remarkReadingTime } from './src/plugins/remark-reading-time.ts'
|
||||||
|
|
||||||
const { url }: { url: ThemeConfig['site']['url'] } = themeConfig.site
|
const { url }: { url: ThemeConfig['site']['url'] } = themeConfig.site
|
||||||
|
@ -54,26 +55,37 @@ export default defineConfig({
|
||||||
remarkMath,
|
remarkMath,
|
||||||
remarkReadingTime,
|
remarkReadingTime,
|
||||||
remarkExcerpt,
|
remarkExcerpt,
|
||||||
remarkGithubAdmonitions,
|
remarkGithubAdmonitionsToDirectives,
|
||||||
remarkDirective,
|
remarkDirective,
|
||||||
remarkDirectiveRehype,
|
|
||||||
remarkSectionize,
|
remarkSectionize,
|
||||||
parseDirectiveNode,
|
parseDirectiveNode,
|
||||||
],
|
],
|
||||||
rehypePlugins: [
|
rehypePlugins: [
|
||||||
rehypeKatex,
|
rehypeKatex,
|
||||||
rehypeSlug,
|
rehypeSlug,
|
||||||
|
[
|
||||||
rehypePrettyCode,
|
rehypePrettyCode,
|
||||||
|
{
|
||||||
|
theme: 'github-dark',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
rehypeExternalLinks,
|
||||||
|
{
|
||||||
|
target: '_blank',
|
||||||
|
rel: ['nofollow', 'noopener', 'noreferrer'],
|
||||||
|
},
|
||||||
|
],
|
||||||
[
|
[
|
||||||
rehypeComponents,
|
rehypeComponents,
|
||||||
{
|
{
|
||||||
components: {
|
components: {
|
||||||
github: GithubCardComponent,
|
github: GithubCardComponent,
|
||||||
note: (x: any, y: any) => AdmonitionComponent(x, y, 'note'),
|
note: (props, children) => AdmonitionComponent(props, 'note', children),
|
||||||
tip: (x: any, y: any) => AdmonitionComponent(x, y, 'tip'),
|
tip: (props, children) => AdmonitionComponent(props, 'tip', children),
|
||||||
important: (x: any, y: any) => AdmonitionComponent(x, y, 'important'),
|
important: (props, children) => AdmonitionComponent(props, 'important', children),
|
||||||
caution: (x: any, y: any) => AdmonitionComponent(x, y, 'caution'),
|
caution: (props, children) => AdmonitionComponent(props, 'caution', children),
|
||||||
warning: (x: any, y: any) => AdmonitionComponent(x, y, 'warning'),
|
warning: (props, children) => AdmonitionComponent(props, 'warning', children),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
@ -91,17 +103,16 @@ export default defineConfig({
|
||||||
'className': ['anchor-icon'],
|
'className': ['anchor-icon'],
|
||||||
'data-pagefind-ignore': true,
|
'data-pagefind-ignore': true,
|
||||||
},
|
},
|
||||||
children: [{ type: 'text', value: '#' }],
|
children: [
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
[
|
|
||||||
rehypeExternalLinks,
|
|
||||||
{
|
{
|
||||||
target: '_blank',
|
type: 'text',
|
||||||
rel: ['nofollow', 'noopener', 'noreferrer'],
|
value: '#',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,26 +1,21 @@
|
||||||
import type { Root } from 'mdast'
|
import type { Root } from 'mdast'
|
||||||
|
import type { VFile } from 'vfile'
|
||||||
import { toString } from 'mdast-util-to-string'
|
import { toString } from 'mdast-util-to-string'
|
||||||
|
|
||||||
interface AstroData {
|
|
||||||
data: {
|
|
||||||
astro: {
|
|
||||||
frontmatter: {
|
|
||||||
excerpt: string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function remarkExcerpt() {
|
export function remarkExcerpt() {
|
||||||
return (tree: Root, file: AstroData) => {
|
return function (tree: Root, file: VFile) {
|
||||||
let excerpt = ''
|
let excerpt = ''
|
||||||
for (const node of tree.children) {
|
for (const node of tree.children) {
|
||||||
if (node.type !== 'paragraph') {
|
if (node.type === 'paragraph') {
|
||||||
continue
|
|
||||||
}
|
|
||||||
excerpt = toString(node)
|
excerpt = toString(node)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保 data.astro.frontmatter 存在
|
||||||
|
file.data.astro = file.data.astro || {}
|
||||||
|
file.data.astro.frontmatter = file.data.astro.frontmatter || {}
|
||||||
|
|
||||||
file.data.astro.frontmatter.excerpt = excerpt
|
file.data.astro.frontmatter.excerpt = excerpt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,18 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue