-
Notifications
You must be signed in to change notification settings - Fork 860
experiment: use typedoc's own html instead of markdown #3410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking "Sign up for GitHub", you agree to our terms of service and privacy statement. We'll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
+507
-338
Draft
experiment: use typedoc's own html instead of markdown #3410
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
688e011
initial work to use html typedoc
vasfvitor 25b4dac
fix usage + improve stuff
vasfvitor b3ff438
Update .gitignore
vasfvitor 110861d
fix sourceLinkTemplate
vasfvitor 2780136
generate sidebar
vasfvitor 5267daf
temp disble link validation
vasfvitor 5e3bf57
fixyo + format
vasfvitor d4ae4c1
Merge branch 'v2' into poc-typedoc-html
vasfvitor 10699a0
Update pnpm-lock.yaml
vasfvitor b6b93ce
Update _generated-javascript-reference-sidebar.js
vasfvitor e3ed0f6
Update build.ts
vasfvitor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,3 +19,5 @@ pnpm-debug.log* | |
|
|
||
| # macOS-specific files | ||
| .DS_Store | ||
| packages/js-api-generator/build.js | ||
| /public/reference/javascript | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,11 @@ pnpm-lock.yaml | |
| .github | ||
| !.github/**.md | ||
|
|
||
| # Generated files | ||
| public/generated/* | ||
| src/content/docs/release | ||
| src/_generated-javascript-reference-sidebar.js | ||
|
|
||
| # TODO: Figure out why this format isn't acceptable | ||
| # https://github.com/withastro/prettier-plugin-astro/issues/407 | ||
| src/components/overrides/Header.astro | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import path from 'path'; | |
| import { fileURLToPath } from 'url'; | ||
| import lunaria from '@lunariajs/starlight'; | ||
| import { readFileSync } from 'fs'; | ||
| import referenceJavascriptSidebar from './src/_generated-javascript-reference-sidebar.js'; | ||
|
|
||
| const authors = { | ||
| nothingismagick: { | ||
|
|
@@ -292,9 +293,9 @@ export default defineConfig({ | |
| autogenerate: { directory: 'release' }, | ||
| }, | ||
| { | ||
| label: 'JavaScript', | ||
| collapsed: true, | ||
| autogenerate: { directory: 'reference/javascript' }, | ||
| label: 'JavaScript (TypeDoc)', | ||
| // generated by js-api-generator | ||
| items: referenceJavascriptSidebar, | ||
| }, | ||
| { | ||
| label: 'Rust (docs.rs)', | ||
|
|
@@ -319,11 +320,11 @@ export default defineConfig({ | |
| }, | ||
| } | ||
| ), | ||
| starlightLinksValidator({ | ||
| errorOnFallbackPages: false, | ||
| errorOnRelativeLinks: false, | ||
| exclude: ['/plugin/*/#default-permission', '/plugin/*/#permission-table'], | ||
| }), | ||
| // starlightLinksValidator({ | ||
| // errorOnFallbackPages: false, | ||
| // errorOnRelativeLinks: false, | ||
| // exclude: ['/plugin/*/#default-permission', '/plugin/*/#permission-table'], | ||
| // }), | ||
| lunaria({ configPath: './lunaria.config.json', route: '/contribute/translate-status' }), | ||
| ], | ||
| title: 'Tauri', | ||
|
|
||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,41 @@ | ||
| import { | ||
| Application, | ||
| DeclarationReflection, | ||
| Options, | ||
| PageEvent, | ||
| Reflection, | ||
| SignatureReflection, | ||
| TSConfigReader, | ||
| type TypeDocOptions, | ||
| } from 'typedoc'; | ||
| import { | ||
| MarkdownPageEvent, | ||
| MarkdownTheme, | ||
| MarkdownThemeContext, | ||
| type MarkdownApplication, | ||
| type PluginOptions, | ||
| } from 'typedoc-plugin-markdown'; | ||
| import { existsSync, readFileSync, writeFileSync } from 'node:fs'; | ||
|
|
||
|
const typeDocConfigBaseOptions: Partial |
||
| import { existsSync, mkdirSync, writeFileSync, readdirSync, readFileSync, cpSync } from 'node:fs'; | ||
| import { resolve, join, dirname } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { Application, DefaultTheme, LogLevel, TSConfigReader, type TypeDocOptions } from 'typedoc'; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
|
|
||
| const BASE_OUTPUT_DIR = resolve(__dirname, '../../public/reference/javascript'); | ||
|
|
||
| const OUTPUT_DOCS_SRC_DIR = resolve(__dirname, '../../src'); | ||
|
|
||
|
const typeDocConfigBaseOptions: Partial |
||
| // TypeDoc options | ||
| // https://typedoc.org/options/ | ||
| githubPages: false, | ||
| hideGenerator: true, | ||
| theme: 'tauri-theme', | ||
| plugin: ['typedoc-plugin-mdn-links', 'typedoc-plugin-markdown'], | ||
| plugin: ['typedoc-plugin-mdn-links'], | ||
| readme: 'none', | ||
| logLevel: 'Warn', | ||
| parametersFormat: 'table', | ||
| // typedoc-plugin-markdown options | ||
| // https://github.com/tgreyuk/typedoc-plugin-markdown/blob/next/packages/typedoc-plugin-markdown/docs/usage/options.md | ||
| outputFileStrategy: 'modules', | ||
| flattenOutputFiles: true, | ||
| entryFileName: 'index.md', | ||
| hidePageHeader: true, | ||
| hidePageTitle: true, | ||
| hideBreadcrumbs: true, | ||
| useCodeBlocks: true, | ||
| propertiesFormat: 'table', | ||
| typeDeclarationFormat: 'table', | ||
| useHTMLAnchors: true, | ||
| logLevel: LogLevel.Warn, | ||
| includeVersion: true, | ||
| searchInComments: true, | ||
| navigationLinks: { | ||
| 'Tauri Docs': 'https://tauri.app', | ||
| GitHub: 'https://github.com/tauri-apps/tauri', | ||
| }, | ||
| visibilityFilters: { | ||
| protected: true, | ||
| private: false, | ||
| inherited: true, | ||
| external: false, | ||
| }, | ||
| categorizeByGroup: true, | ||
| cleanOutputDir: true, | ||
| // disableSources: false, | ||
| sort: ['source-order'], | ||
| highlightLanguages: ['typescript', 'javascript', 'json', 'bash', 'shell', 'rust', 'toml'], | ||
| }; | ||
|
|
||
| async function generator() { | ||
|
|
@@ -47,8 +44,8 @@ async function generator() { | |
| entryPoints: ['../tauri/packages/api/src/index.ts'], | ||
| tsconfig: '../tauri/packages/api/tsconfig.json', | ||
| gitRevision: 'dev', | ||
| publicPath: '/reference/javascript/api/', | ||
| basePath: '/reference/javascript/api/', | ||
| out: join(BASE_OUTPUT_DIR, 'core'), | ||
| name: 'Tauri Core API', | ||
| ...typeDocConfigBaseOptions, | ||
| }; | ||
|
|
||
|
|
@@ -100,152 +97,122 @@ async function generator() { | |
| { encoding: 'utf8' } | ||
| ); | ||
|
|
||
| plugins.forEach(async (plugin) => { | ||
| const pluginsPromises = plugins.map(async (plugin) => { | ||
|
const pluginJsOptions: Partial |
||
| entryPoints: [`../plugins-workspace/plugins/${plugin}/guest-js/index.ts`], | ||
| tsconfig: `../plugins-workspace/plugins/${plugin}/tsconfig.json`, | ||
| gitRevision: 'v2', | ||
| publicPath: `/reference/javascript/`, | ||
| basePath: `/reference/javascript/`, | ||
| out: join(BASE_OUTPUT_DIR, 'plugins', plugin), | ||
| name: `@tauri-apps/plugin-${plugin}`, | ||
| ...typeDocConfigBaseOptions, | ||
| // Must go after to override base | ||
| entryFileName: `${plugin}.md`, | ||
| }; | ||
|
|
||
| await generateDocs(pluginJsOptions); | ||
| return generateDocs(pluginJsOptions); | ||
| }); | ||
| } else { | ||
| console.log( | ||
| 'Plugins workspace submodule is not initialized, respective API routes will not be rendered.' | ||
| ); | ||
| } | ||
|
|
||
| if (existsSync('../tauri/packages/api/node_modules')) { | ||
|
const coreJsOptions: Partial |
||
| entryPoints: ['../tauri/packages/api/src/index.ts'], | ||
| tsconfig: '../tauri/packages/api/tsconfig.json', | ||
| gitRevision: 'dev', | ||
| publicPath: '/reference/javascript/api/', | ||
| basePath: '/reference/javascript/api/', | ||
| ...typeDocConfigBaseOptions, | ||
| }; | ||
|
|
||
| await generateDocs(coreJsOptions); | ||
| await Promise.all(pluginsPromises); | ||
| } else { | ||
| console.log( | ||
| 'Tauri V2 submodule is not initialized, respective API routes will not be rendered.' | ||
| 'Plugins workspace submodule is not initialized, respective API routes will not be rendered.' | ||
| ); | ||
| } | ||
| await generateIndexPage(); | ||
| } | ||
|
|
||
| // Adapted from https://github.com/HiDeoo/starlight-typedoc | ||
|
async function generateDocs(options: Partial |
||
| const outputDir = `../../src/content/docs${options.publicPath}`; | ||
| // console.log(`Generating docs for ${options.name}`); | ||
|
|
||
| const outDir = options.out as string; | ||
| if (!existsSync(outDir)) { | ||
| mkdirSync(outDir, { recursive: true }); | ||
| } | ||
| const app = await Application.bootstrapWithPlugins(options); | ||
| app.options.addReader(new TSConfigReader()); | ||
| // @ts-ignore | ||
| app.renderer.defineTheme('tauri-theme', TauriTheme); | ||
|
|
||
|
app.renderer.on(PageEvent.END, (event: PageEvent |
||
| pageEventEnd(event); | ||
| }); | ||
| options.theme = 'tauri-theme'; | ||
| app.renderer.defineTheme('tauri-theme', TauriDefaultTheme); | ||
|
|
||
| const project = await app.convert(); | ||
|
|
||
| if (project) { | ||
| await app.generateDocs(project, outputDir); | ||
| if (!project) { | ||
| throw new Error(`Failed to convert project: ${options.name}`); | ||
| } | ||
| } | ||
|
|
||
| // Adds frontmatter to the top of the file | ||
| // Adapted from https://github.com/HiDeoo/starlight-typedoc | ||
|
function pageEventEnd(event: PageEvent |
||
| if (!event.contents) { | ||
| return; | ||
| } | ||
| const frontmatter = [ | ||
| '---', | ||
| `title: "${event.model.name}"`, | ||
| 'editUrl: false', | ||
| 'sidebar:', | ||
| ` label: "${event.model.name.replace('@tauri-apps/plugin-', '')}"`, | ||
| 'tableOfContents:', | ||
| ' maxHeadingLevel: 5', | ||
| '---', | ||
| '', | ||
| event.contents, | ||
| ]; | ||
| event.contents = frontmatter.join('\n'); | ||
| await app.generateDocs(project, outDir); | ||
| } | ||
| class TauriThemeRenderContext extends MarkdownThemeContext { | ||
|
constructor(theme: MarkdownTheme, page: MarkdownPageEvent |
||
| super(theme, page, options); | ||
| const originalCommentPartial = this.partials.comment; | ||
|
|
||
| this.partials = { | ||
| ...this.partials, | ||
| // Formats `@source` to be a single line | ||
| sources: (model: DeclarationReflection | SignatureReflection, options: object) => { | ||
| if (!model.sources) { | ||
| return ''; | ||
| } | ||
| let label = model.sources.length > 1 ? '**Sources**: ' : '**Source**: '; | ||
| const sources = model.sources.map((source) => `${source.url}`); | ||
| return label + sources.join(', '); | ||
| }, | ||
| // Remove heading markers from JSDoc comments to prevent accidental markdown headings | ||
| comment: function (comment, options) { | ||
| const headingStringsToReplace = [ | ||
| // known to break | ||
| '#### Platform-specific', | ||
| // just to be sure | ||
| '### Platform-specific', | ||
| ]; | ||
|
|
||
| if (comment?.summary) { | ||
| comment.summary.forEach((line) => { | ||
| if (line.kind === 'text' && typeof line.text === 'string') { | ||
| headingStringsToReplace.forEach((headingString) => { | ||
| line.text = line.text.replace(headingString, headingString.replace(/^#+\s*/, '')); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| if (comment?.blockTags) { | ||
| comment.blockTags.forEach((tag) => { | ||
| tag.content.forEach((line) => { | ||
| if (line.kind === 'text' && typeof line.text === 'string') { | ||
| headingStringsToReplace.forEach((headingString) => { | ||
| line.text = line.text.replace(headingString, headingString.replace(/^#+\s*/, '')); | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| return originalCommentPartial.call(this, comment, options); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| // Adapted from https://github.com/HiDeoo/starlight-typedoc/blob/d95072e218004276942a5132ec8a4e3561425903/packages/starlight-typedoc/src/libs/theme.ts#L28 | ||
| override getRelativeUrl = (url: string) => { | ||
| if (/^(http|ftp)s?:\/\//.test(url)) { | ||
| return url; | ||
| } | ||
| async function generateIndexPage() { | ||
| const indexPath = join(BASE_OUTPUT_DIR, 'index.html'); | ||
|
|
||
| url = decodeURI( | ||
| super.getRelativeUrl(url).replaceAll('.md', '/').replaceAll('.', '').toLowerCase() | ||
| ).replaceAll('\\', '/'); | ||
| return url; | ||
| const cardTemplate = (name: string, path: string) => { | ||
| return ` | ||
${name} |
||
| View | ||
| `; | ||
| }; | ||
| } | ||
|
|
||
| // Overrides and extensions based on https://github.com/tgreyuk/typedoc-plugin-markdown/blob/next/packages/typedoc-plugin-markdown/docs/usage/customizing.md | ||
| class TauriTheme extends MarkdownTheme { | ||
|
getRenderContext(page: MarkdownPageEvent |
||
| return new TauriThemeRenderContext(this, page, this.application.options); | ||
| let pluginsGridHtml; | ||
| if (existsSync(join(BASE_OUTPUT_DIR, 'plugins'))) { | ||
| const pluginDirs = readdirSync(join(BASE_OUTPUT_DIR, 'plugins')); | ||
|
|
||
| pluginsGridHtml = pluginDirs | ||
| .map((plugin: string) => cardTemplate(plugin, `./plugins/${plugin}/index.html`)) | ||
| .join(''); | ||
| } | ||
|
|
||
| // TODO: improve layout | ||
| // TODO: improve theme switcher | ||
| // TODO: link to docs | ||
| // TODO: make docs link to here | ||
| const indexTemplatePath = join(__dirname, 'indexTemplate.html'); | ||
| const indexContent = readFileSync(indexTemplatePath, 'utf-8') | ||
| .replace('{{ pluginsGridHtml }}', pluginsGridHtml || '') | ||
| .replace('{{ tauriCard }}', cardTemplate('Tauri Core API', '/reference/javascript/core/')); | ||
|
|
||
| const assetsDir = join(BASE_OUTPUT_DIR, 'assets'); | ||
| if (!existsSync(assetsDir)) { | ||
| mkdirSync(assetsDir, { recursive: true }); | ||
| } | ||
| const distAssetsDir = join(__dirname, 'assets'); | ||
| if (existsSync(distAssetsDir)) { | ||
| try { | ||
| cpSync(distAssetsDir, assetsDir, { recursive: true, force: true }); | ||
| } catch (err) { | ||
| console.error('Failed to copy assets:', err); | ||
| } | ||
| } else { | ||
| console.warn(`Assets directory not found at ${distAssetsDir}`); | ||
| } | ||
| try { | ||
| writeFileSync(indexPath, indexContent); | ||
| // Starlight topics especific sidebar structure | ||
| const sidebar = [ | ||
| { | ||
| label: 'Index', | ||
| link: '/reference/javascript/index.html', | ||
| }, | ||
| { | ||
| label: 'Tauri Core API', | ||
| link: '/reference/javascript/core/index.html', | ||
| }, | ||
| ...(pluginsGridHtml && existsSync(join(BASE_OUTPUT_DIR, 'plugins')) | ||
| ? readdirSync(join(BASE_OUTPUT_DIR, 'plugins')).map((plugin) => ({ | ||
| label: plugin, | ||
| link: `/reference/javascript/plugins/${plugin}/index.html`, | ||
| })) | ||
| : []), | ||
| ]; | ||
| const sidebarFilePath = join(OUTPUT_DOCS_SRC_DIR, '_generated-javascript-reference-sidebar.js'); | ||
| writeFileSync(sidebarFilePath, 'export default ' + JSON.stringify(sidebar, null, 2) + ';\n'); | ||
| } catch (error) { | ||
| console.error('Failed to write index files:', error); | ||
| } | ||
| } | ||
|
|
||
| generator(); | ||
| class TauriDefaultTheme extends DefaultTheme {} | ||
|
|
||
| generator().catch((error) => { | ||
| console.error('Failed to generate documentation:', error); | ||
| process.exit(1); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.