From 812aa3bf9735bf07bf8615b3cf85d02e53cd1558 Mon Sep 17 00:00:00 2001 From: Neko Ayaka Date: Fri, 17 May 2024 19:05:10 +0800 Subject: [PATCH] feat(meta): added new vitepress-plugin-meta package (#218) --- README.md | 1 + README.zh-CN.md | 1 + cspell.config.yaml | 3 + docs/.vitepress/config.ts | 225 +++++------------- docs/.vitepress/twoslashConfig.ts | 47 ++++ docs/package.json | 1 + docs/pages/en/index.md | 9 + docs/pages/en/integrations/index.md | 11 + .../vitepress-plugin-meta/index.md | 35 +++ docs/pages/zh-CN/index.md | 9 + docs/pages/zh-CN/integrations/index.md | 11 + .../vitepress-plugin-meta/index.md | 35 +++ packages/vitepress-plugin-meta/README.md | 27 +++ .../vitepress-plugin-meta/build.config.ts | 16 ++ packages/vitepress-plugin-meta/package.json | 74 ++++++ .../src/vitepress/index.test.ts | 135 +++++++++++ .../src/vitepress/index.ts | 94 ++++++++ .../src/vitepress/testdata/index.en.html | 56 +++++ .../src/vitepress/testdata/index.zh-CN.html | 47 ++++ packages/vitepress-plugin-meta/tsconfig.json | 35 +++ .../src/vitepress/index.ts | 2 - pnpm-lock.yaml | 101 ++++++++ 22 files changed, 810 insertions(+), 165 deletions(-) create mode 100644 docs/.vitepress/twoslashConfig.ts create mode 100644 docs/pages/en/integrations/vitepress-plugin-meta/index.md create mode 100644 docs/pages/zh-CN/integrations/vitepress-plugin-meta/index.md create mode 100644 packages/vitepress-plugin-meta/README.md create mode 100644 packages/vitepress-plugin-meta/build.config.ts create mode 100644 packages/vitepress-plugin-meta/package.json create mode 100644 packages/vitepress-plugin-meta/src/vitepress/index.test.ts create mode 100644 packages/vitepress-plugin-meta/src/vitepress/index.ts create mode 100644 packages/vitepress-plugin-meta/src/vitepress/testdata/index.en.html create mode 100644 packages/vitepress-plugin-meta/src/vitepress/testdata/index.zh-CN.html create mode 100644 packages/vitepress-plugin-meta/tsconfig.json diff --git a/README.md b/README.md index eb6e7be9..8d4035be 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ To find out the integration name, there is a list of integrations below. - [Inline Link Previewing (for VitePress)](./packages/vitepress-plugin-inline-link-preview/README.md) - [Blinking highlight targeted heading (for VitePress)](./packages/vitepress-plugin-highlight-targeted-heading/README.md) - [Page properties (for VitePress)](./packages/vitepress-plugin-page-properties/README.md) +- [Page `` metadata generation](./packages/vitepress-plugin-meta/README.md) - [Previewing image (social media card) generation (for VitePress)](./packages/vitepress-plugin-og-image/README.md) - [Enhanced `` element (for VitePress)](./packages/vitepress-plugin-enhanced-mark/README.md) - [Thumbnail hashing for images (for VitePress)](./packages/vitepress-plugin-thumbnail-hash/README.md) diff --git a/README.zh-CN.md b/README.zh-CN.md index 652ec72b..b090b24b 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -73,6 +73,7 @@ ni @nolebase/ -D - [闪烁高亮当前的目标标题(VitePress 插件)](./packages/vitepress-plugin-highlight-targeted-heading/README.md) - [页面属性(VitePress 插件)](./packages/vitepress-plugin-page-properties/README.md) - [预览图片(社交媒体卡片)生成(VitePress 插件)](./packages/vitepress-plugin-og-image/README.md) +- [页面 `` 元信息生成](./packages/vitepress-plugin-meta/README.md) - [`` 元素增强(VitePress 插件)](./packages/vitepress-plugin-enhanced-mark/README.md) - [缩略图模糊哈希生成(VitePress 插件)](./packages/vitepress-plugin-thumbnail-hash/README.md) diff --git a/cspell.config.yaml b/cspell.config.yaml index 7281cad6..a196626e 100644 --- a/cspell.config.yaml +++ b/cspell.config.yaml @@ -21,6 +21,7 @@ words: - composables - contributor - datetime + - defu - easytag - ElMassimo - emittypes @@ -45,9 +46,11 @@ words: - quotepath - Rehype - resvg + - retext - rizumu - Roboto - shikijs + - testdata - thumbhash - thumbhashes - Twoslash diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 7cd92867..823e1fdf 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -1,4 +1,4 @@ -import { cwd, env } from 'node:process' +import { argv, cwd, env } from 'node:process' import { gray } from 'colorette' import { type DefaultTheme, defineConfig } from 'vitepress' @@ -10,8 +10,14 @@ import { BiDirectionalLinks } from '@nolebase/markdown-it-bi-directional-links' import { InlineLinkPreviewElementTransform } from '@nolebase/vitepress-plugin-inline-link-preview/markdown-it' import { buildEndGenerateOpenGraphImages } from '@nolebase/vitepress-plugin-og-image/vitepress' import { UnlazyImages } from '@nolebase/markdown-it-unlazy-img' +import { transformHeadMeta } from '@nolebase/vitepress-plugin-meta/vitepress' import packageJSON from '../../package.json' +import { compilerOptions } from './twoslashConfig' + +function noTwoslash() { + return argv.some(v => v.includes('vitepress')) && argv.includes('dev') +} export const sidebars: Record = { 'en': { @@ -22,61 +28,6 @@ export const sidebars: Record = { { text: 'Getting Started', link: '/pages/en/guide/getting-started' }, ], }, - { - text: 'Integrations', - items: [ - { text: 'Overview', link: '/pages/en/integrations/' }, - { - text: 'Markdown-it plugins', - items: [ - { text: 'Bi-directional links', link: '/pages/en/integrations/markdown-it-bi-directional-links/' }, - { text: 'Element Transformation', link: '/pages/en/integrations/markdown-it-element-transform/' }, - { text: 'Lazy loading blurred thumbnails', link: '/pages/en/integrations/markdown-it-unlazy-img/' }, - ], - }, - { - text: 'VitePress plugins', - items: [ - { text: 'Enhanced Readabilities', link: '/pages/en/integrations/vitepress-plugin-enhanced-readabilities/' }, - { text: 'Inline Links Previewing', link: '/pages/en/integrations/vitepress-plugin-inline-link-preview/' }, - { text: 'Blinking highlight targeted heading', link: '/pages/en/integrations/vitepress-plugin-highlight-targeted-heading/' }, - { text: 'Git-based page histories', link: '/pages/en/integrations/vitepress-plugin-git-changelog/' }, - { text: 'Page properties', link: '/pages/en/integrations/vitepress-plugin-page-properties/' }, - { text: 'Previewing image (social media card) generation', link: '/pages/en/integrations/vitepress-plugin-og-image/' }, - { text: 'Enhanced mark elements', link: '/pages/en/integrations/vitepress-plugin-enhanced-mark/' }, - { text: 'Thumbnail hashing for images', link: '/pages/en/integrations/vitepress-plugin-thumbnail-hash/' }, - ], - }, - { - text: 'Obsidian plugins', - items: [ - { text: 'UnoCSS', link: '/pages/en/integrations/obsidian-plugin-unocss/' }, - ], - }, - ], - }, - { - text: 'UI Components', - items: [ - { text: 'Overview', link: '/pages/en/ui/' }, - ], - }, - { - text: 'Releasing', - items: [ - { - text: 'Migration guides', - items: [ - { - text: 'Migrate from v1 to v2', - link: '/pages/en/releases/migrations/v1-to-v2', - }, - ], - }, - ], - }, - ], - '/pages/en/integrations/': [ { text: 'Integrations', items: [ @@ -124,6 +75,7 @@ export const sidebars: Record = { ], }, { text: 'Page properties', link: '/pages/en/integrations/vitepress-plugin-page-properties/' }, + { text: 'Page metadata generation', link: '/pages/en/integrations/vitepress-plugin-meta/' }, { text: 'Previewing image (social media card) generation', link: '/pages/en/integrations/vitepress-plugin-og-image/' }, { text: 'Enhanced mark elements', link: '/pages/en/integrations/vitepress-plugin-enhanced-mark/' }, { @@ -142,6 +94,26 @@ export const sidebars: Record = { { text: 'UnoCSS', link: '/pages/en/integrations/obsidian-plugin-unocss/' }, ], }, + { + text: 'UI Components', + items: [ + { text: 'Overview', link: '/pages/en/ui/' }, + ], + }, + { + text: 'Releasing', + items: [ + { + text: 'Migration guides', + items: [ + { + text: 'Migrate from v1 to v2', + link: '/pages/en/releases/migrations/v1-to-v2', + }, + ], + }, + ], + }, ], '/pages/en/ui/': [ { @@ -180,61 +152,6 @@ export const sidebars: Record = { { text: '如何开始', link: '/pages/zh-CN/guide/getting-started' }, ], }, - { - text: '集成', - items: [ - { text: '概览', link: '/pages/zh-CN/integrations/' }, - { - text: 'Markdown It 插件', - items: [ - { text: '双向链接', link: '/pages/zh-CN/integrations/markdown-it-bi-directional-links/' }, - { text: '元素转换', link: '/pages/zh-CN/integrations/markdown-it-element-transform/' }, - { text: '懒加载模糊缩略图', link: '/pages/zh-CN/integrations/markdown-it-unlazy-img/' }, - ], - }, - { - text: 'VitePress 插件', - items: [ - { text: '阅读增强', link: '/pages/zh-CN/integrations/vitepress-plugin-enhanced-readabilities/' }, - { text: '行内链接预览', link: '/pages/zh-CN/integrations/vitepress-plugin-inline-link-preview/' }, - { text: '闪烁高亮当前的目标标题', link: '/pages/zh-CN/integrations/vitepress-plugin-highlight-targeted-heading/' }, - { text: '基于 Git 的页面历史', link: '/pages/zh-CN/integrations/vitepress-plugin-git-changelog/' }, - { text: '页面属性', link: '/pages/zh-CN/integrations/vitepress-plugin-page-properties/' }, - { text: '预览图片(社交媒体卡片)生成', link: '/pages/zh-CN/integrations/vitepress-plugin-og-image/' }, - { text: 'mark 元素增强', link: '/pages/zh-CN/integrations/vitepress-plugin-enhanced-mark/' }, - { text: '缩略图模糊哈希生成', link: '/pages/zh-CN/integrations/vitepress-plugin-thumbnail-hash/' }, - ], - }, - { - text: 'Obsidian 插件', - items: [ - { text: 'UnoCSS', link: '/pages/zh-CN/integrations/obsidian-plugin-unocss/' }, - ], - }, - ], - }, - { - text: 'UI 组件', - items: [ - { text: '概览', link: '/pages/zh-CN/ui/' }, - ], - }, - { - text: '版本发布', - items: [ - { - text: '迁移指南', - items: [ - { - text: '自 v1 迁移至 v2', - link: '/pages/zh-CN/releases/migrations/v1-to-v2', - }, - ], - }, - ], - }, - ], - '/pages/zh-CN/integrations/': [ { text: '集成', items: [ @@ -282,6 +199,7 @@ export const sidebars: Record = { ], }, { text: '页面属性', link: '/pages/zh-CN/integrations/vitepress-plugin-page-properties/' }, + { text: '页面元信息生成', link: '/pages/zh-CN/integrations/vitepress-plugin-meta/' }, { text: '预览图片(社交媒体卡片)生成', link: '/pages/zh-CN/integrations/vitepress-plugin-og-image/' }, { text: 'mark 元素增强', link: '/pages/zh-CN/integrations/vitepress-plugin-enhanced-mark/' }, { @@ -300,6 +218,20 @@ export const sidebars: Record = { { text: 'UnoCSS', link: '/pages/zh-CN/integrations/obsidian-plugin-unocss/' }, ], }, + { + text: '版本发布', + items: [ + { + text: '迁移指南', + items: [ + { + text: '自 v1 迁移至 v2', + link: '/pages/zh-CN/releases/migrations/v1-to-v2', + }, + ], + }, + ], + }, ], '/pages/zh-CN/ui/': [ { @@ -466,59 +398,17 @@ export default defineConfig({ }, }, markdown: { - codeTransformers: [ - transformerTwoslash({ - errorRendering: 'hover', - onTwoslashError(error, _, __, ___) { - console.error('Twoslash Error:', (error as Error)?.message, '\n', (error as Error)?.stack ? gray(String((error as Error)?.stack)) : '') - }, - twoslashOptions: { - cache: true, - compilerOptions: { - baseUrl: cwd(), - target: 99, - module: 99, - moduleResolution: 100, - paths: { - '@nolebase/ui': [ - '../packages/ui/src/index.ts', - ], - '@nolebase/unconfig-vitepress/*': [ - '../packages/unconfig-vitepress/src/*', - ], - '@nolebase/vitepress-plugin-enhanced-readabilities/*': [ - '../packages/vitepress-plugin-enhanced-readabilities/src/*', - ], - '@nolebase/vitepress-plugin-highlight-targeted-heading/*': [ - '../packages/vitepress-plugin-highlight-targeted-heading/src/*', - ], - '@nolebase/vitepress-plugin-inline-link-preview/*': [ - '../packages/vitepress-plugin-inline-link-preview/src/*', - ], - '@nolebase/vitepress-plugin-git-changelog/*': [ - '../packages/vitepress-plugin-git-changelog/src/*', - ], - '@nolebase/vitepress-plugin-page-properties/*': [ - '../packages/vitepress-plugin-page-properties/src/*', - ], - '@nolebase/vitepress-plugin-thumbnail-hash/*': [ - '../packages/vitepress-plugin-thumbnail-hash/src/*', - ], + codeTransformers: noTwoslash() + ? [] + : [ + transformerTwoslash({ + errorRendering: 'hover', + onTwoslashError(error, _, __, ___) { + console.error('Twoslash Error:', (error as Error)?.message, '\n', (error as Error)?.stack ? gray(String((error as Error)?.stack)) : '') }, - resolveJsonModule: true, - types: [ - 'node', - 'vite/client', - ], - esModuleInterop: true, - isolatedModules: true, - verbatimModuleSyntax: true, - skipLibCheck: true, - skipDefaultLibCheck: true, - }, - }, - }), - ], + twoslashOptions: compilerOptions, + }), + ], preConfig(md) { md.use(BiDirectionalLinks({ dir: cwd(), @@ -532,6 +422,15 @@ export default defineConfig({ md.use(InlineLinkPreviewElementTransform) }, }, + async transformHead(context) { + let head = [...context.head] + + const returnedHead = await transformHeadMeta()(head, context) + if (typeof returnedHead !== 'undefined') + head = returnedHead + + return head + }, async buildEnd(siteConfig) { const newBuilder = buildEndGenerateOpenGraphImages({ baseUrl: 'https://nolebase-integrations.ayaka.io', diff --git a/docs/.vitepress/twoslashConfig.ts b/docs/.vitepress/twoslashConfig.ts new file mode 100644 index 00000000..18ae048b --- /dev/null +++ b/docs/.vitepress/twoslashConfig.ts @@ -0,0 +1,47 @@ +import { cwd } from 'node:process' + +export const compilerOptions = { + cache: true, + compilerOptions: { + baseUrl: cwd(), + target: 99, + module: 99, + moduleResolution: 100, + paths: { + '@nolebase/ui': [ + '../packages/ui/src/index.ts', + ], + '@nolebase/unconfig-vitepress/*': [ + '../packages/unconfig-vitepress/src/*', + ], + '@nolebase/vitepress-plugin-enhanced-readabilities/*': [ + '../packages/vitepress-plugin-enhanced-readabilities/src/*', + ], + '@nolebase/vitepress-plugin-highlight-targeted-heading/*': [ + '../packages/vitepress-plugin-highlight-targeted-heading/src/*', + ], + '@nolebase/vitepress-plugin-inline-link-preview/*': [ + '../packages/vitepress-plugin-inline-link-preview/src/*', + ], + '@nolebase/vitepress-plugin-git-changelog/*': [ + '../packages/vitepress-plugin-git-changelog/src/*', + ], + '@nolebase/vitepress-plugin-page-properties/*': [ + '../packages/vitepress-plugin-page-properties/src/*', + ], + '@nolebase/vitepress-plugin-thumbnail-hash/*': [ + '../packages/vitepress-plugin-thumbnail-hash/src/*', + ], + }, + resolveJsonModule: true, + types: [ + 'node', + 'vite/client', + ], + esModuleInterop: true, + isolatedModules: true, + verbatimModuleSyntax: true, + skipLibCheck: true, + skipDefaultLibCheck: true, + }, +} diff --git a/docs/package.json b/docs/package.json index 21879da4..9064f098 100644 --- a/docs/package.json +++ b/docs/package.json @@ -27,6 +27,7 @@ "@nolebase/vitepress-plugin-graph-view": "workspace:^", "@nolebase/vitepress-plugin-highlight-targeted-heading": "workspace:^", "@nolebase/vitepress-plugin-inline-link-preview": "workspace:^", + "@nolebase/vitepress-plugin-meta": "workspace:^", "@nolebase/vitepress-plugin-og-image": "workspace:^", "@nolebase/vitepress-plugin-page-properties": "workspace:^", "@nolebase/vitepress-plugin-thumbnail-hash": "workspace:^", diff --git a/docs/pages/en/index.md b/docs/pages/en/index.md index 6c317304..457e25e1 100644 --- a/docs/pages/en/index.md +++ b/docs/pages/en/index.md @@ -83,6 +83,15 @@ Nólëbase Integrations project provides a variety of integrations, plugins, com + + + + +