]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
docs: upgrade api docs generation
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 1 Aug 2023 18:16:11 +0000 (20:16 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 1 Aug 2023 18:16:11 +0000 (20:16 +0200)
No longer need to manually edit duplicated links

package.json
packages/docs/.vitepress/config/shared.ts
packages/docs/package.json
packages/docs/typedoc-markdown.js

index a611511c097e9c2c81d6e07450f282d968408276..daaf2b25e85095ed9826af31a7c6817b0ac4dc14 100644 (file)
@@ -35,7 +35,7 @@
     "prettier": "^2.8.8",
     "semver": "^7.5.4",
     "typedoc": "^0.24.8",
-    "typedoc-plugin-markdown": "^3.15.3",
+    "typedoc-plugin-markdown": "^3.15.4",
     "typescript": "~5.1.6",
     "yorkie": "^2.0.0"
   },
index a028d2f0a3db1f7385f1b807b7a47731bb6f9024..cf84a4f605d94ee72a350df467df8358a75763b5 100644 (file)
@@ -35,6 +35,10 @@ export const sharedConfig = defineConfig({
       leftDelimiter: '%{',
       rightDelimiter: '}%',
     },
+
+    anchor: {
+      slugify: s => s.replace(/\s/g, '-'),
+    },
   },
 
   head: [
index b676dcbdf7328f310aeb1e20ca67e08781e3e66c..940f7f641fbb6ec70135553bcd6cb9a599b74938 100644 (file)
@@ -8,7 +8,7 @@
     "docs:build": "vitepress build ."
   },
   "dependencies": {
-    "vitepress": "1.0.0-alpha.76",
+    "vitepress": "1.0.0-beta.7",
     "vue-router": "workspace:*"
   }
 }
index 6cc4f04fffd818d361d7f5faecf93a7e6a2c6f11..4e4d4ce3e6c81e64d73611ceb8e63c519233e2aa 100644 (file)
@@ -2,9 +2,6 @@ const _fs = require('fs')
 const path = require('path')
 const TypeDoc = require('typedoc')
 const { PageEvent } = TypeDoc
-const {
-  prependYAML,
-} = require('typedoc-plugin-markdown/dist/utils/front-matter')
 
 const fs = _fs.promises
 
@@ -15,6 +12,7 @@ const DEFAULT_OPTIONS = {
   readme: 'none',
   out: path.resolve(__dirname, './api'),
   entryDocument: 'index.md',
+  preserveAnchorCasing: true,
   hideBreadcrumbs: false,
   hideInPageTOC: true,
 }
@@ -33,13 +31,6 @@ exports.createTypeDocApp = function createTypeDocApp(config = {}) {
 
   // If you want TypeDoc to load tsconfig.json / typedoc.json files
   app.options.addReader(new TypeDoc.TSConfigReader())
-  // app.options.addReader(new TypeDoc.TypeDocReader())
-
-  /** @type {'build' | 'serve'} */
-  let targetMode = 'build'
-
-  const slugify = s => s.replaceAll(' ', '-')
-  // encodeURIComponent(String(s).trim().toLowerCase().replace(/\s+/g, '-'))
 
   app.renderer.on(
     PageEvent.END,
@@ -48,55 +39,10 @@ exports.createTypeDocApp = function createTypeDocApp(config = {}) {
      * @param {import('typedoc/dist/lib/output/events').PageEvent} page
      */
     page => {
-      if (page.url !== 'index.md' && page.contents) {
-        page.contents = prependYAML(page.contents, {
-          // TODO: figure out a way to point to the source files?
-          editLink: false,
-        })
-      }
-
-      // avoid duplicated id titles
-      if (page.contents) {
-        const lines = page.contents.split('\n')
-        const titleStack = []
-        let currentLevel = 0
-        const TITLE_LEVEL = /^#+/
-        const existingIds = new Map()
-        for (let i = 0; i < lines.length; i++) {
-          const line = lines[i]
-          if (!line.startsWith('#')) continue
-          const level = line.match(TITLE_LEVEL)[0].length
-
-          // remove extra levels
-          if (level <= currentLevel) {
-            titleStack.splice(level - 1)
-          }
-          // add the current title
-          titleStack.push(line.slice(level).trim())
-          currentLevel = level
-
-          // no need to add ids to h1
-          if (level < 2) continue
-
-          // ignore the root level (h1) to match the sidebar
-          const slugifiedTitle = slugify(titleStack.slice(1).join('-'))
-            // ensure the link is valid #1743
-            .replaceAll('\\', '')
-          let id
-          if (existingIds.has(slugifiedTitle)) {
-            const current = existingIds.get(slugifiedTitle)
-            existingIds.set(slugifiedTitle, current + 1)
-            id = ` %{#${slugifiedTitle}_${current + 1}}%`
-          } else {
-            existingIds.set(slugifiedTitle, 0)
-            id = ` %{#${slugifiedTitle}}%`
-          }
-          const newLine = line + id
-          lines.splice(i, 1, newLine)
-        }
-
-        page.contents = lines.join('\n')
-      }
+      page.contents = prependYAML(page.contents, {
+        // TODO: figure out a way to point to the source files?
+        editLink: false,
+      })
     }
   )
 
@@ -156,3 +102,43 @@ async function exists(path) {
     return false
   }
 }
+/**
+ * @typedef {Record<string, string | number | boolean>} FrontMatterVars
+ */
+
+/**
+ * Prepends YAML block to a string
+ * @param {string} contents - string to prepend to
+ * @param {FrontMatterVars} vars - object of required front matter variables
+ */
+function prependYAML(contents, vars) {
+  return contents
+    .replace(/^/, toYAML(vars) + '\n\n')
+    .replace(/[\r\n]{3,}/g, '\n\n')
+}
+
+/**
+ * Converts YAML object to a YAML string
+ * @param {FrontMatterVars} vars
+ */
+function toYAML(vars) {
+  const yaml = `---
+${Object.entries(vars)
+  .map(
+    ([key, value]) =>
+      `${key}: ${
+        typeof value === 'string' ? `"${escapeDoubleQuotes(value)}"` : value
+      }`
+  )
+  .join('\n')}
+---`
+  return yaml
+}
+
+/**
+ * Escapes double quotes in a string
+ * @param {string} str - string to escape
+ */
+function escapeDoubleQuotes(str) {
+  return str.replace(/"/g, '\\"')
+}