Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisjofrank committed Nov 25, 2024
1 parent 6e128d6 commit 94f1deb
Showing 8 changed files with 539 additions and 534 deletions.
1 change: 0 additions & 1 deletion _includes/doc.tsx
Original file line number Diff line number Diff line change
@@ -199,4 +199,3 @@ export default function Page(props: Lume.Data, helpers: Lume.Helpers) {
</>
);
}

336 changes: 174 additions & 162 deletions _includes/renderCommand.tsx
Original file line number Diff line number Diff line change
@@ -6,8 +6,20 @@ import CLI_REFERENCE from "../runtime/reference/cli/_commands_reference.json" wi
};
import { VNode } from "npm:preact";

type ArgType = { name: string; help: string; short: string | number | bigint | boolean | object | VNode<any> | null | undefined; };

type ArgType = {
name: string;
help: string;
short:
| string
| number
| bigint
| boolean
| object
| VNode<any>
| null
| undefined;
};

const ANSI_RE = ansiRegex();
const SUBSEQUENT_ANSI_RE = new RegExp(
`(?:${ANSI_RE.source})(?:${ANSI_RE.source})`,
@@ -31,172 +43,172 @@ function flagsToInlineCode(text: string): string {
}

export default function renderCommand(
commandName: string,
helpers: Lume.Helpers,
): { rendered: any; toc: TableOfContentsItem_[] } {
const command = CLI_REFERENCE.subcommands.find((command) =>
command.name === commandName
)!;

const toc: TableOfContentsItem_[] = [];

let about = command.about!.replaceAll(
SUBSEQUENT_ENCAPSULATED_ANSI_RE,
function (
_,
_opening1,
text1,
_closing1,
space,
opening2,
text2,
closing2,
) {
return `${opening2}${text1}${space}${text2}${closing2}`;
},
).replaceAll(SUBSEQUENT_ANSI_RE, "");
let aboutLines = about.split("\n");
const aboutLinesReadMoreIndex = aboutLines.findLastIndex((line) =>
line.toLowerCase().replaceAll(ANSI_RE, "").trim().startsWith("read more:")
);
if (aboutLinesReadMoreIndex !== -1) {
aboutLines = aboutLines.slice(0, aboutLinesReadMoreIndex);
}

about = aboutLines.join("\n").replaceAll(
ENCAPSULATED_ANSI_RE,
(_, opening, text, _closing, offset, string) => {
if (opening === "\u001b[32m") { // green, used as heading
return `### ${text}`;
} else if (
opening === "\u001b[38;5;245m" || opening === "\u001b[36m" ||
opening === "\u001b[1m" || opening === "\u001b[22m"
) { // gray and cyan used for code and snippets, and we treat yellow and bold as well as such
const lines = string.split("\n");
let line = "";

while (offset > 0) {
line = lines.shift();
offset -= line.length;
}

if (START_AND_END_ANSI_RE.test(line.trim())) {
return "\n```\n" + text + "\n```\n\n";
} else {
return "`" + text + "`";
}
commandName: string,
helpers: Lume.Helpers,
): { rendered: any; toc: TableOfContentsItem_[] } {
const command = CLI_REFERENCE.subcommands.find((command) =>
command.name === commandName
)!;

const toc: TableOfContentsItem_[] = [];

let about = command.about!.replaceAll(
SUBSEQUENT_ENCAPSULATED_ANSI_RE,
function (
_,
_opening1,
text1,
_closing1,
space,
opening2,
text2,
closing2,
) {
return `${opening2}${text1}${space}${text2}${closing2}`;
},
).replaceAll(SUBSEQUENT_ANSI_RE, "");
let aboutLines = about.split("\n");
const aboutLinesReadMoreIndex = aboutLines.findLastIndex((line) =>
line.toLowerCase().replaceAll(ANSI_RE, "").trim().startsWith("read more:")
);
if (aboutLinesReadMoreIndex !== -1) {
aboutLines = aboutLines.slice(0, aboutLinesReadMoreIndex);
}

about = aboutLines.join("\n").replaceAll(
ENCAPSULATED_ANSI_RE,
(_, opening, text, _closing, offset, string) => {
if (opening === "\u001b[32m") { // green, used as heading
return `### ${text}`;
} else if (
opening === "\u001b[38;5;245m" || opening === "\u001b[36m" ||
opening === "\u001b[1m" || opening === "\u001b[22m"
) { // gray and cyan used for code and snippets, and we treat yellow and bold as well as such
const lines = string.split("\n");
let line = "";

while (offset > 0) {
line = lines.shift();
offset -= line.length;
}

if (START_AND_END_ANSI_RE.test(line.trim())) {
return "\n```\n" + text + "\n```\n\n";
} else {
return text;
return "`" + text + "`";
}
},
);

const args = [];
const options: Record<string, ArgType[]> = {};

for (const arg of command.args) {
if (arg.help_heading === "Unstable options") {
continue;
}

if (arg.long) {
const key = arg.help_heading ?? "Options";
options[key] ??= [];
options[key].push(arg);
} else {
args.push(arg);
return text;
}
},
);

const args = [];
const options: Record<string, ArgType[]> = {};

for (const arg of command.args) {
if (arg.help_heading === "Unstable options") {
continue;
}

if (arg.long) {
const key = arg.help_heading ?? "Options";
options[key] ??= [];
options[key].push(arg);
} else {
args.push(arg);
}

const rendered = (
<div>
<div class="p-4 bg-stone-100 dark:bg-transparent rounded border border-gray-300 dark:border-background-tertiary mt-6 mb-6 relative">
<h3 class="!text-xs !m-0 -top-2.5 bg-background-primary border border-gray-600/25 px-2 py-0.5 rounded absolute !font-normal">
Command line usage
</h3>
<div>
<pre class="!mb-0 !px-3 !py-2">
}

const rendered = (
<div>
<div class="p-4 bg-stone-100 dark:bg-transparent rounded border border-gray-300 dark:border-background-tertiary mt-6 mb-6 relative">
<h3 class="!text-xs !m-0 -top-2.5 bg-background-primary border border-gray-600/25 px-2 py-0.5 rounded absolute !font-normal">
Command line usage
</h3>
<div>
<pre class="!mb-0 !px-3 !py-2">
<code>{command.usage.replaceAll(ANSI_RE, "").slice("usage: ".length)}</code>
</pre>
</div>
</pre>
</div>

<div dangerouslySetInnerHTML={{ __html: helpers.md(about) }} />
<br />

{Object.entries(options).map(([heading, flags]) => {
const id = heading.toLowerCase().replace(/\s/g, "-");

const renderedFlags = flags.toSorted((a: ArgType, b: ArgType) =>
a.name.localeCompare(b.name)
).map((flag: ArgType) => renderOption(id, flag, helpers));

toc.push({
text: heading,
slug: id,
children: [],
});

return (
<>
<h2 id={id}>
{heading} <HeaderAnchor id={id} />
</h2>
{renderedFlags}
</>
);
})}
</div>
);

return {
rendered,
toc,
};
}

function renderOption(group: string, arg: ArgType, helpers: Lume.Helpers) {
const id = `${group}-${arg.name}`;

let docsLink = null;
let help = arg.help.replaceAll(ANSI_RE, "");
const helpLines = help.split("\n");
const helpLinesDocsIndex = helpLines.findLastIndex((line) =>
line.toLowerCase()
.trim()
.startsWith("docs:")
);
if (helpLinesDocsIndex !== -1) {
help = helpLines.slice(0, helpLinesDocsIndex).join("\n");
docsLink = helpLines[helpLinesDocsIndex].trim().slice("docs:".length);
}

return (
<>
<h3 id={id}>
<code>
{docsLink
? <a href={docsLink}>{"--" + arg.name}</a>
: ("--" + arg.name)}
</code>{" "}
<HeaderAnchor id={id} />
</h3>
{arg.short && (
<p>
Short flag: <code>-{arg.short}</code>
</p>
)}
{arg.help && (
<p
class="block !whitespace-pre-line"
dangerouslySetInnerHTML={{
__html: helpers.md(
flagsToInlineCode(help) +
((help.endsWith(".") || help.endsWith("]")) ? "" : "."),
),
}}
/>
)}
</>
);

<div dangerouslySetInnerHTML={{ __html: helpers.md(about) }} />
<br />

{Object.entries(options).map(([heading, flags]) => {
const id = heading.toLowerCase().replace(/\s/g, "-");

const renderedFlags = flags.toSorted((a: ArgType, b: ArgType) =>
a.name.localeCompare(b.name)
).map((flag: ArgType) => renderOption(id, flag, helpers));

toc.push({
text: heading,
slug: id,
children: [],
});

return (
<>
<h2 id={id}>
{heading} <HeaderAnchor id={id} />
</h2>
{renderedFlags}
</>
);
})}
</div>
);

return {
rendered,
toc,
};
}

function renderOption(group: string, arg: ArgType, helpers: Lume.Helpers) {
const id = `${group}-${arg.name}`;

let docsLink = null;
let help = arg.help.replaceAll(ANSI_RE, "");
const helpLines = help.split("\n");
const helpLinesDocsIndex = helpLines.findLastIndex((line) =>
line.toLowerCase()
.trim()
.startsWith("docs:")
);
if (helpLinesDocsIndex !== -1) {
help = helpLines.slice(0, helpLinesDocsIndex).join("\n");
docsLink = helpLines[helpLinesDocsIndex].trim().slice("docs:".length);
}

return (
<>
<h3 id={id}>
<code>
{docsLink
? <a href={docsLink}>{"--" + arg.name}</a>
: ("--" + arg.name)}
</code>{" "}
<HeaderAnchor id={id} />
</h3>
{arg.short && (
<p>
Short flag: <code>-{arg.short}</code>
</p>
)}
{arg.help && (
<p
class="block !whitespace-pre-line"
dangerouslySetInnerHTML={{
__html: helpers.md(
flagsToInlineCode(help) +
((help.endsWith(".") || help.endsWith("]")) ? "" : "."),
),
}}
/>
)}
</>
);
}
Loading

0 comments on commit 94f1deb

Please sign in to comment.