Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/components/GitHubCode.astro
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,20 @@ if (!res.ok) {
throw new Error(`[GitHubCode] Received ${res.status} from Worker.`);
}

let content = await res.text();
const content = await res.text();
let contentLines = content.split("\n");

if (lines) {
const [start, end] = lines;

const contentLines = content.split("\n");

if (contentLines.length < end - 1) {
throw new Error(
`[GitHubCode] End line requested is beyond content length (${contentLines.length}).`,
);
}

content = contentLines.slice(start - 1, end).join("\n");
contentLines = contentLines.slice(start - 1, end);
} else if (tag) {
const contentLines = content.split("\n");

const startTag = contentLines.findIndex((x) =>
x.includes(`<docs-tag name="${tag}">`),
);
Expand All @@ -61,8 +58,12 @@ if (lines) {
throw new Error(`[GitHubCode] Unable to find a region using tag "${tag}".`);
}

content = contentLines.slice(startTag + 1, endTag).join("\n");
contentLines = contentLines.slice(startTag + 1, endTag);
}

contentLines = contentLines.filter(
(line) => !/<[\/]?docs-tag name=".*">/.test(line),
);
---

<Code code={content} lang={lang} />
<Code code={contentLines.join("\n")} lang={lang} />
8 changes: 4 additions & 4 deletions src/content/docs/style-guide/components/github-code.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import { GitHubCode } from "~/components";
import { GitHubCode } from "~/components";

<GitHubCode
repo="cloudflare/workers-rs"
file="templates/hello-world/src/lib.rs"
commit="ab3951b5c95329a600a7baa9f9bb1a7a95f1aeaa"
lang="rs"
repo="cloudflare/workflows-starter"
file="src/index.ts"
commit="a844e629ec80968118d4b116d4b26f5dcb107137"
lang="ts"
/>
```

Expand Down
Loading