|
| 1 | +import { CommandError, createCommand, type CommandConfig } from "../lib/command"; |
| 2 | +import { stringify } from "../lib/json"; |
| 3 | + |
| 4 | +const DOCS_BASE_URL = new URL("https://prismic.io/docs/"); |
| 5 | + |
| 6 | +const config = { |
| 7 | + name: "prismic docs view", |
| 8 | + description: ` |
| 9 | + View a documentation page as Markdown. |
| 10 | +
|
| 11 | + Append #anchor to the path to view only the section under that heading. |
| 12 | + `, |
| 13 | + positionals: { |
| 14 | + path: { |
| 15 | + description: "Documentation path, optionally with #anchor (e.g., setup#install)", |
| 16 | + required: true, |
| 17 | + }, |
| 18 | + }, |
| 19 | + options: { |
| 20 | + json: { type: "boolean", description: "Output as JSON" }, |
| 21 | + }, |
| 22 | +} satisfies CommandConfig; |
| 23 | + |
| 24 | +export default createCommand(config, async ({ positionals, values }) => { |
| 25 | + const [rawPath] = positionals; |
| 26 | + const { json } = values; |
| 27 | + |
| 28 | + const hashIndex = rawPath.indexOf("#"); |
| 29 | + const path = hashIndex >= 0 ? rawPath.slice(0, hashIndex) : rawPath; |
| 30 | + const anchor = hashIndex >= 0 ? rawPath.slice(hashIndex + 1) : undefined; |
| 31 | + |
| 32 | + const url = new URL(path, DOCS_BASE_URL); |
| 33 | + const response = await fetch(url, { |
| 34 | + headers: { Accept: "text/markdown" }, |
| 35 | + }); |
| 36 | + |
| 37 | + if (!response.ok) { |
| 38 | + throw new CommandError(`Failed to fetch documentation page: ${response.statusText}`); |
| 39 | + } |
| 40 | + |
| 41 | + let markdown = await response.text(); |
| 42 | + |
| 43 | + if (anchor) { |
| 44 | + const section = extractSection(markdown, anchor); |
| 45 | + if (!section) { |
| 46 | + throw new CommandError(`Anchor not found: #${anchor}`); |
| 47 | + } |
| 48 | + markdown = section; |
| 49 | + } |
| 50 | + |
| 51 | + if (json) { |
| 52 | + console.info(stringify({ path, anchor, content: markdown })); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + console.info(markdown); |
| 57 | +}); |
| 58 | + |
| 59 | +function extractSection(markdown: string, anchor: string): string | undefined { |
| 60 | + const lines = markdown.split("\n"); |
| 61 | + let startIndex = -1; |
| 62 | + let headingLevel = 0; |
| 63 | + |
| 64 | + for (let i = 0; i < lines.length; i++) { |
| 65 | + const match = lines[i].match(/^(#{1,6})\s+(.*)/); |
| 66 | + if (!match) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + const level = match[1].length; |
| 71 | + const text = match[2]; |
| 72 | + |
| 73 | + if (startIndex >= 0 && level <= headingLevel) { |
| 74 | + return lines.slice(startIndex, i).join("\n").trimEnd(); |
| 75 | + } |
| 76 | + |
| 77 | + if (kebabCase(text) === anchor) { |
| 78 | + startIndex = i; |
| 79 | + headingLevel = level; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (startIndex >= 0) { |
| 84 | + return lines.slice(startIndex).join("\n").trimEnd(); |
| 85 | + } |
| 86 | + |
| 87 | + return undefined; |
| 88 | +} |
| 89 | + |
| 90 | +function kebabCase(text: string): string { |
| 91 | + return text |
| 92 | + .toLowerCase() |
| 93 | + .replace(/[^a-z0-9]+/g, "-") |
| 94 | + .replace(/^-|-$/g, ""); |
| 95 | +} |
0 commit comments