-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dd47b9
commit c8b95e1
Showing
4 changed files
with
278 additions
and
88 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,99 +1,13 @@ | ||
--- | ||
import { Code } from "@astrojs/starlight/components"; | ||
import { redirectToDefaultLocale } from "astro:i18n"; | ||
import fs from "node:fs/promises"; | ||
import sampleCode from './utils/sample'; | ||
const { path, type, token1 = "", token2 = "" } = Astro.props; | ||
let code = await fs.readFile(path, "utf-8"); | ||
let partialCode = code; | ||
let symbols = []; | ||
if (token1 != "") { | ||
symbols.push(token1); | ||
} | ||
if (token2 != "") { | ||
symbols.push(token2); | ||
} | ||
if (symbols.length != 0) { | ||
let snippets = []; | ||
for (let symbol of symbols) { | ||
const match = code.match(new RegExp(`^([\\s]*)(${symbol})`, "m")); | ||
if (!match) { | ||
throw new Error(`Symbol not found: ${symbol}`); | ||
} | ||
// Determine the end of the symbol. | ||
let start = match.index; | ||
let ix, block_token; | ||
try { | ||
[ix, block_token] = [ | ||
...[":", "{", ";"].map((token) => [ | ||
code.indexOf(token, match.index), | ||
token, | ||
]), | ||
] | ||
.filter(([loc]) => loc !== -1) | ||
.sort((a, b) => a[0] - b[0])[0]; | ||
} catch (e) { | ||
throw new Error( | ||
`No block character (:, {) found after ${symbol} at line ${code.slice(0, start).split("\n").length - 1}` | ||
); | ||
} | ||
// Push the start marker backwards to include any leading comments. | ||
let lines = code.slice(0, start).split("\n"); | ||
for (let line of lines.reverse()) { | ||
if (/^[\s]*(\/\/|#)/.test(line)) { | ||
start -= line.length + 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
// Handle block types based on the token found. | ||
let snippet = ""; | ||
if (block_token === ":") { | ||
const indent = match[1].length; | ||
const endMatch = code | ||
.slice(ix) | ||
.match(new RegExp(`^[\\s]{0,${indent}}[^\\s]`, "m")); | ||
snippet = endMatch | ||
? code.slice(start, ix + endMatch.index) | ||
: code.slice(start); | ||
} else if (block_token === "{") { | ||
let cursor = match.index; | ||
while (true) { | ||
const close_brace = code.indexOf("}", cursor); | ||
if (close_brace === -1) { | ||
throw new Error( | ||
`No corresponding } found for ${symbol} at line ${code.slice(0, start).split("\n").length - 1}` | ||
); | ||
} | ||
const s = match.index, | ||
e = close_brace + 1; | ||
if ( | ||
code.slice(s, e).split("{").length === | ||
code.slice(s, e).split("}").length | ||
) { | ||
snippet = code.slice(start, e); | ||
break; | ||
} | ||
cursor = e; | ||
} | ||
} else { | ||
const end = code.indexOf(";", match.index) + 1; | ||
snippet = code.slice(start, end); | ||
} | ||
// Append the snippet to the list of snippets. | ||
snippets.push(snippet.trim()); | ||
} | ||
// We have a snippet. Time to put the Markdown together. | ||
partialCode = snippets.join("\n\n"); | ||
} | ||
let partialCode = sampleCode(code, type, token1, token2); | ||
--- | ||
|
||
<Code code={partialCode} lang={type} /> |
Oops, something went wrong.