typedoc2md / typedoc-plugin-markdown

A plugin for TypeDoc that enables TypeScript API documentation to be generated in Markdown.
https://typedoc-plugin-markdown.org
MIT License
689 stars 172 forks source link

Renamed Files Aren't Tracked in Index.md File #631

Closed crutchcorn closed 1 week ago

crutchcorn commented 2 weeks ago

What package is the bug related to?

typedoc-plugin-markdown

Describe the issue

If I add the following custom plugin:

import { MarkdownPageEvent } from 'typedoc-plugin-markdown'
import path from 'node:path'

/**
 * @param {import("typedoc-plugin-markdown").MarkdownApplication} app
 */
export function load(app) {
  app.renderer.on(
    MarkdownPageEvent.BEGIN,
    /**
     * @param {import("typedoc-plugin-markdown").MarkdownPageEvent} page
     */
    (page) => {
      const dirname = path.dirname(page.filename)
      const basename = path.basename(page.filename)
      const name = basename.split('.')
      if (name[0] !== 'index') {
        name.splice(0, 1)
      }
      const newBasename = name.join('.').toLowerCase();
      page.filename = path.join(dirname, newBasename)
    },
  )
}

That I use to rename files to match our needs for TanStack Form, then it will generate the following index.md file:

# @tanstack/angular-form

## Classes

- [TanStackField](Class.TanStackField.md)

## Functions

- [injectForm](Function.injectForm.md)
- [injectStore](Function.injectStore.md)

Despite the file paths being as such:

index.md
injectform.md
injectstore.md
tanstackfield.md

TypeDoc configuration

https://github.com/TanStack/form/pull/735/files#diff-3c52ddc750be1c8a30bf81c638ff98b62579ee9a3b446d703497f8cbd50dbb17

Expected behavior

The index.md paths should match the final output of the file names themselves

tgreyuk commented 1 week ago

@crutchcorn The index.md pages have already been written before the event gets triggered on individual page. You need to update all the urls before the pages start to render.

You can do this with MarkdownRendererEvent.BEGIN.

Try this:

app.renderer.on(
    MarkdownRendererEvent.BEGIN,
    /**
     * @param {import("typedoc-plugin-markdown").MarkdownRendererEvent} renderer
     */ (renderer) => {
      renderer.urls = renderer.urls?.map((urlMapping) => {
        const name = urlMapping.url.split(".");
        if (name[0] !== "index") {
          name.splice(0, 1);
        }
        const newBasename = name.join(".").toLowerCase();
        urlMapping.url = newBasename;
        urlMapping.model.url = newBasename;
        return urlMapping;
      });
    }
  );
crutchcorn commented 1 week ago

This is awesome, thank you so much for your help!