Skip to content

Commit

Permalink
json path ish support (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
rambleraptor authored Nov 1, 2024
1 parent 7dd47b9 commit c8b95e1
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 88 deletions.
147 changes: 147 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@astrojs/tailwind": "^5.1.0",
"@hpcc-js/wasm": "^2.22.1",
"astro": "^4.10.2",
"jsonpath": "^1.1.1",
"marked": "^14.1.2",
"rehype-graphviz": "^0.3.0",
"sharp": "^0.32.5",
Expand Down
90 changes: 2 additions & 88 deletions src/components/Sample.astro
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} />
Loading

0 comments on commit c8b95e1

Please sign in to comment.