-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add docs command for browsing Prismic documentation
#85
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
angeloashmore
wants to merge
1
commit into
main
Choose a base branch
from
aa/docs-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−1
Draft
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { stringify } from "../lib/json"; | ||
|
|
||
| const DOCS_INDEX_URL = new URL("https://prismic.io/docs/api/index/"); | ||
|
|
||
| const config = { | ||
| name: "prismic docs list", | ||
| description: ` | ||
| List available documentation pages. | ||
|
|
||
| With a path argument, list the anchors within that page. | ||
| `, | ||
| positionals: { | ||
| path: { | ||
| description: "Documentation path to list anchors for", | ||
| required: false, | ||
| }, | ||
| }, | ||
| options: { | ||
| json: { type: "boolean", description: "Output as JSON" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| type IndexPage = { | ||
| path: string; | ||
| title: string; | ||
| description: string; | ||
| }; | ||
|
|
||
| type IndexPageWithAnchors = IndexPage & { | ||
| anchors: { slug: string; excerpt: string }[]; | ||
| }; | ||
|
|
||
| export default createCommand(config, async ({ positionals, values }) => { | ||
| const [path] = positionals; | ||
| const { json } = values; | ||
|
|
||
| if (path) { | ||
| const url = new URL(path, DOCS_INDEX_URL); | ||
| const response = await fetch(url); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 404) { | ||
| throw new CommandError(`Documentation page not found: ${path}`); | ||
| } | ||
| throw new CommandError(`Failed to fetch documentation index: ${response.statusText}`); | ||
| } | ||
|
|
||
| const entry: IndexPageWithAnchors = await response.json(); | ||
| entry.anchors.sort((a, b) => a.slug.localeCompare(b.slug)); | ||
|
|
||
| if (json) { | ||
| console.info(stringify(entry)); | ||
| return; | ||
| } | ||
|
|
||
| if (entry.anchors.length === 0) { | ||
| console.info("(no anchors)"); | ||
| return; | ||
| } | ||
|
|
||
| for (const anchor of entry.anchors) { | ||
| console.info(`${path}#${anchor.slug}: ${anchor.excerpt}`); | ||
| } | ||
| } else { | ||
| const response = await fetch(DOCS_INDEX_URL); | ||
|
|
||
| if (!response.ok) { | ||
| throw new CommandError(`Failed to fetch documentation index: ${response.statusText}`); | ||
| } | ||
|
|
||
| const pages: IndexPage[] = await response.json(); | ||
| pages.sort((a, b) => a.path.localeCompare(b.path)); | ||
|
|
||
| if (json) { | ||
| console.info(stringify(pages)); | ||
| return; | ||
| } | ||
|
|
||
| if (pages.length === 0) { | ||
| console.info("No documentation pages found."); | ||
| return; | ||
| } | ||
|
|
||
| for (const page of pages) { | ||
| console.info(`${page.path}: ${page.title} — ${page.description}`); | ||
| } | ||
| } | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { stringify } from "../lib/json"; | ||
|
|
||
| const DOCS_BASE_URL = new URL("https://prismic.io/docs/"); | ||
|
|
||
| const config = { | ||
| name: "prismic docs view", | ||
| description: ` | ||
| View a documentation page as Markdown. | ||
|
|
||
| Append #anchor to the path to view only the section under that heading. | ||
| `, | ||
| positionals: { | ||
| path: { | ||
| description: "Documentation path, optionally with #anchor (e.g., setup#install)", | ||
| required: true, | ||
| }, | ||
| }, | ||
| options: { | ||
| json: { type: "boolean", description: "Output as JSON" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ positionals, values }) => { | ||
| const [rawPath] = positionals; | ||
| const { json } = values; | ||
|
|
||
| const hashIndex = rawPath.indexOf("#"); | ||
| const path = hashIndex >= 0 ? rawPath.slice(0, hashIndex) : rawPath; | ||
| const anchor = hashIndex >= 0 ? rawPath.slice(hashIndex + 1) : undefined; | ||
|
|
||
| const url = new URL(path, DOCS_BASE_URL); | ||
| const response = await fetch(url, { | ||
| headers: { Accept: "text/markdown" }, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new CommandError(`Failed to fetch documentation page: ${response.statusText}`); | ||
| } | ||
|
|
||
| let markdown = await response.text(); | ||
|
|
||
| if (anchor) { | ||
| const section = extractSection(markdown, anchor); | ||
| if (!section) { | ||
| throw new CommandError(`Anchor not found: #${anchor}`); | ||
| } | ||
| markdown = section; | ||
| } | ||
|
|
||
| if (json) { | ||
| console.info(stringify({ path, anchor, content: markdown })); | ||
| return; | ||
| } | ||
|
|
||
| console.info(markdown); | ||
| }); | ||
|
|
||
| function extractSection(markdown: string, anchor: string): string | undefined { | ||
| const lines = markdown.split("\n"); | ||
| let startIndex = -1; | ||
| let headingLevel = 0; | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const match = lines[i].match(/^(#{1,6})\s+(.*)/); | ||
| if (!match) { | ||
| continue; | ||
| } | ||
|
|
||
| const level = match[1].length; | ||
| const text = match[2]; | ||
|
|
||
| if (startIndex >= 0 && level <= headingLevel) { | ||
| return lines.slice(startIndex, i).join("\n").trimEnd(); | ||
| } | ||
|
|
||
| if (kebabCase(text) === anchor) { | ||
| startIndex = i; | ||
| headingLevel = level; | ||
| } | ||
| } | ||
|
|
||
| if (startIndex >= 0) { | ||
| return lines.slice(startIndex).join("\n").trimEnd(); | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| function kebabCase(text: string): string { | ||
| return text | ||
| .toLowerCase() | ||
| .replace(/[^a-z0-9]+/g, "-") | ||
| .replace(/^-|-$/g, ""); | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { createCommandRouter } from "../lib/command"; | ||
|
|
||
| import docsList from "./docs-list"; | ||
| import docsView from "./docs-view"; | ||
|
|
||
| export default createCommandRouter({ | ||
| name: "prismic docs", | ||
| description: "Browse Prismic documentation.", | ||
| commands: { | ||
| list: { | ||
| handler: docsList, | ||
| description: "List available documentation pages", | ||
| }, | ||
| view: { | ||
| handler: docsView, | ||
| description: "View a documentation page", | ||
| }, | ||
| }, | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Section extraction ignores fenced code blocks
High Severity
extractSectiontreats every line matching the heading regex as a real heading, including lines inside fenced code blocks. Developer documentation very commonly contains#comment lines in bash/shell/Python code blocks. When extracting a section, a# commentinside a code block is misidentified as a level-1 heading, triggering thelevel <= headingLeveltermination condition and truncating the section prematurely. The function needs to track fenced code block state (``` delimiters) and skip heading detection inside them.