diff --git a/blocksuite/docs-site/.vitepress/config.ts b/blocksuite/docs-site/.vitepress/config.ts index 33bceddaa..f49535fc3 100644 --- a/blocksuite/docs-site/.vitepress/config.ts +++ b/blocksuite/docs-site/.vitepress/config.ts @@ -120,6 +120,14 @@ export default defineConfig({ search: { provider: 'local', + options: { + _render(src, env, md) { + if (env.relativePath.startsWith('api/')) { + return ''; + } + return md.render(src, env); + }, + }, }, }, markdown: { diff --git a/blocksuite/docs-site/.vitepress/sidebar.ts b/blocksuite/docs-site/.vitepress/sidebar.ts index 201124636..b2ea5e8ae 100644 --- a/blocksuite/docs-site/.vitepress/sidebar.ts +++ b/blocksuite/docs-site/.vitepress/sidebar.ts @@ -1,3 +1,6 @@ +import { existsSync, readdirSync } from 'node:fs'; +import { join, parse } from 'node:path'; + import type { DefaultTheme } from 'vitepress'; export const guide: DefaultTheme.NavItem[] = [ @@ -100,17 +103,48 @@ export const guide: DefaultTheme.NavItem[] = [ export const reference: DefaultTheme.NavItem[] = [ { text: 'API Reference', - items: [ - { text: '@blocksuite/store', link: 'api/@blocksuite/store/index' }, - { - text: '@blocksuite/block-std', - link: 'api/@blocksuite/block-std/index', - }, - { text: '@blocksuite/inline', link: 'api/@blocksuite/inline/index' }, - ], + items: getApiReferenceItems(), }, ]; +function getApiReferenceItems(): DefaultTheme.NavItem[] { + const apiDir = join(process.cwd(), 'api', '@blocksuite'); + + if (!existsSync(apiDir)) { + return [ + { text: '@blocksuite/store', link: 'api/@blocksuite/store' }, + { text: '@blocksuite/std', link: 'api/@blocksuite/std/index' }, + { text: '@blocksuite/affine', link: 'api/@blocksuite/affine' }, + ]; + } + + return readdirSync(apiDir, { withFileTypes: true }) + .flatMap(entry => { + if (entry.isFile() && entry.name.endsWith('.md')) { + const name = parse(entry.name).name; + return [ + { text: `@blocksuite/${name}`, link: `api/@blocksuite/${name}` }, + ]; + } + + if (entry.isDirectory()) { + const indexPath = join(apiDir, entry.name, 'index.md'); + + if (existsSync(indexPath)) { + return [ + { + text: `@blocksuite/${entry.name}`, + link: `api/@blocksuite/${entry.name}/index`, + }, + ]; + } + } + + return []; + }) + .sort((a, b) => a.text.localeCompare(b.text)); +} + export const components: DefaultTheme.NavItem[] = [ { text: 'Introduction', diff --git a/blocksuite/docs-site/.vitepress/theme/components/Icon.vue b/blocksuite/docs-site/.vitepress/theme/components/icon.vue similarity index 100% rename from blocksuite/docs-site/.vitepress/theme/components/Icon.vue rename to blocksuite/docs-site/.vitepress/theme/components/icon.vue diff --git a/blocksuite/docs-site/copy-pages-worker.mjs b/blocksuite/docs-site/copy-pages-worker.mjs new file mode 100644 index 000000000..3edb5a84a --- /dev/null +++ b/blocksuite/docs-site/copy-pages-worker.mjs @@ -0,0 +1,7 @@ +import { copyFileSync } from 'node:fs'; +import { join } from 'node:path'; + +copyFileSync( + join(process.cwd(), 'pages-worker.mjs'), + join(process.cwd(), '.vitepress', 'dist', '_worker.js') +); diff --git a/blocksuite/docs-site/package.json b/blocksuite/docs-site/package.json index 054388921..f3c4dc3c0 100644 --- a/blocksuite/docs-site/package.json +++ b/blocksuite/docs-site/package.json @@ -8,10 +8,11 @@ "license": "MPL-2.0", "type": "module", "scripts": { - "typedoc": "typedoc --options ./typedoc.json", + "build:deps": "tsc -b ../affine/all", + "typedoc": "yarn run build:deps && typedoc --options ./typedoc.json", "dev": "yarn run typedoc && yarn exec vitepress dev --port 5200", "dev:nobuild": "yarn exec vitepress dev --port 5200", - "build": "yarn run typedoc && NODE_OPTIONS=--max-old-space-size=8192 yarn exec vitepress build", + "build": "yarn run typedoc && NODE_OPTIONS=--max-old-space-size=8192 yarn exec vitepress build && node ./copy-pages-worker.mjs", "preview": "yarn exec vitepress preview" }, "dependencies": { diff --git a/blocksuite/docs-site/pages-worker.mjs b/blocksuite/docs-site/pages-worker.mjs new file mode 100644 index 000000000..528b09827 --- /dev/null +++ b/blocksuite/docs-site/pages-worker.mjs @@ -0,0 +1,40 @@ +const canonicalHost = 'blocksuite.io'; +const redirectHosts = new Set([ + 'blocksuite.affine.pro', + 'block-suite.com', + 'blocksite.dev', + 'blocksite.io', + 'blocksuit.dev', + 'blocksuit.io', +]); +const apiMemberPathPattern = + /^\/api\/@blocksuite\/(.+)\/(classes|enumerations|functions|interfaces|type-aliases|variables)\/[^/]+\.html$/; + +export default { + fetch(request, env) { + const url = new URL(request.url); + + if (redirectHosts.has(url.hostname)) { + url.hostname = canonicalHost; + url.protocol = 'https:'; + + return Response.redirect(url.toString(), 301); + } + + if (url.pathname === '/blocksuite-overview.html') { + url.pathname = '/guide/overview.html'; + + return Response.redirect(url.toString(), 301); + } + + const apiMemberPath = url.pathname.match(apiMemberPathPattern); + + if (apiMemberPath) { + url.pathname = `/api/@blocksuite/${apiMemberPath[1]}.html`; + + return Response.redirect(url.toString(), 301); + } + + return env.ASSETS.fetch(request); + }, +}; diff --git a/blocksuite/docs-site/typedoc-remove-inherited.mjs b/blocksuite/docs-site/typedoc-remove-inherited.mjs new file mode 100644 index 000000000..ea5c8c6f8 --- /dev/null +++ b/blocksuite/docs-site/typedoc-remove-inherited.mjs @@ -0,0 +1,34 @@ +import { Converter } from 'typedoc'; + +export function load(app) { + app.converter.on(Converter.EVENT_RESOLVE_END, context => { + pruneInheritedReflections(context.project); + }); +} + +function pruneInheritedReflections(reflection) { + if (reflection.children) { + reflection.children = reflection.children.filter( + child => !child.inheritedFrom + ); + reflection.children.forEach(pruneInheritedReflections); + } + + if (reflection.groups) { + reflection.groups = reflection.groups + .map(group => ({ + ...group, + children: group.children.filter(child => !child.inheritedFrom), + })) + .filter(group => group.children.length > 0); + } + + if (reflection.categories) { + reflection.categories = reflection.categories + .map(category => ({ + ...category, + children: category.children.filter(child => !child.inheritedFrom), + })) + .filter(category => category.children.length > 0); + } +} diff --git a/blocksuite/docs-site/typedoc.json b/blocksuite/docs-site/typedoc.json index d2764b76e..7671c88ea 100644 --- a/blocksuite/docs-site/typedoc.json +++ b/blocksuite/docs-site/typedoc.json @@ -10,11 +10,13 @@ "packageOptions": { "includeVersion": true, "readme": "none", + "disableSources": true, + "excludeInternal": true, "excludeExternals": true, "externalPattern": ["node_modules/**/*"], "entryPoints": ["src/index.ts"] }, - "plugin": ["typedoc-plugin-markdown"], + "plugin": ["typedoc-plugin-markdown", "./typedoc-remove-inherited.mjs"], "out": "./api", "entryPointStrategy": "packages", "includeVersion": false, @@ -22,8 +24,10 @@ "readme": "none", "name": "BlockSuite API Documentation", "entryFileName": "index.md", - "outputFileStrategy": "members", + "outputFileStrategy": "modules", "hidePageHeader": true, + "disableSources": true, + "excludeInternal": true, "excludePrivate": true, "excludeProtected": true, "excludeExternals": true,