Skip to content

Commit 3af627b

Browse files
Generate config example (#128)
* Make content generation ready for other use-cases * Separate generic genarator from cli option specifics * Fetch example config from main repository * Update config documentation & extract version note * Apply suggestions made by @mre * Apply suggestion by @katrinafyi
1 parent 5eede85 commit 3af627b

File tree

8 files changed

+146
-219
lines changed

8 files changed

+146
-219
lines changed

astro.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import starlight from "@astrojs/starlight";
22
import { defineConfig } from "astro/config";
33
import smartypants from "remark-smartypants";
4-
import { generateCliOptionsIntegration } from "./src/generate-cli-options";
4+
import { generateFilesFromMainRepo } from "./src/generate/generate";
55

66
// https://astro.build/config
77
export default defineConfig({
@@ -14,7 +14,7 @@ export default defineConfig({
1414
],
1515
},
1616
integrations: [
17-
generateCliOptionsIntegration("src/content/docs/guides/_cli.md"),
17+
generateFilesFromMainRepo(),
1818
starlight({
1919
expressiveCode: {
2020
themes: ["catppuccin-frappe", "catppuccin-latte"],

src/content/docs/guides/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/cli.md
2+
/config.md

src/content/docs/guides/_cli.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ lychee --exclude https://example.com --exclude https://example.org README.md
2727

2828
To specify multiple values in this way, the argument flag should be repeated.
2929
Otherwise, the extra values would be treated as link checking inputs.
30-

src/content/docs/guides/_config.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Configuration File
3+
---
4+
5+
The configuration file is a [TOML](https://toml.io) file that can be used to specify the options that are also available on the command line.
6+
It comes in handy when you want to specify a lot of options, or when you want to configure lychee for continuous integration as part of a repository.
7+
8+
By default `lychee.toml` is used if it exists in the current working directory.
9+
If `--config <PATH>` is specified, then the configuration file at the specified path will be used.
10+
11+
```bash
12+
lychee --config config.toml
13+
```
14+
15+
For example, the default value for the `--timeout` option is `20` seconds.
16+
If you want to change this value, you can do so in the configuration file.
17+
18+
```toml title="lychee.toml"
19+
timeout = 30
20+
```
21+
22+
## Example
23+
24+
Here is an example configuration file making use of every single option.
25+
26+
CONFIG-VERSION-NOTE-PLACEHOLDER
27+
28+
```toml title="lychee.toml"
29+
CONFIG-PLACEHOLDER
30+
```

src/content/docs/guides/config.md

Lines changed: 0 additions & 161 deletions
This file was deleted.

src/generate-cli-options.ts renamed to src/generate/generate-cli-options.ts

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import assert from "node:assert";
2-
import { readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
3-
import { basename, dirname, join } from "node:path";
4-
import type { AstroIntegration } from "astro";
5-
import { LYCHEE_VERSION } from "./lychee-version";
6-
7-
// https://raw.githubusercontent.com/lycheeverse/lychee/master/README.md
8-
const url = `https://raw.githubusercontent.com/lycheeverse/lychee/refs/tags/${LYCHEE_VERSION}/README.md`;
9-
10-
const TEMPLATE = "README-OPTIONS-PLACEHOLDER";
2+
import { versionNote as versionGenerationNote } from "./generate";
113

124
function extractHelpFromReadme(readme: string) {
135
const [, section] = readme.split(/### Commandline Parameters/, 2);
@@ -50,12 +42,7 @@ function* generateMarkdown(lines: string[]) {
5042
yield "```bash";
5143
yield line.replace(/^Usage: /, "");
5244
yield "```";
53-
yield `
54-
:::note
55-
This page is up-to-date as of
56-
[${LYCHEE_VERSION}](https://github.com/lycheeverse/lychee/releases/tag/${LYCHEE_VERSION}).
57-
:::
58-
`;
45+
yield versionGenerationNote();
5946
} else if ((match = line.match(headingRegex))) {
6047
yield `## ${match[1]}`;
6148
} else if ((match = line.match(optionRegex))) {
@@ -82,12 +69,9 @@ This page is up-to-date as of
8269
}
8370
// biome-ignore-end lint/suspicious/noAssignInExpressions: using assignment expressions for regex match is conventional
8471

85-
export async function generateCliOptionsMarkdown() {
86-
const readme = await fetch(url);
87-
assert(readme.ok, `${readme.status} when fetching ${url}`);
88-
89-
const rawUsageText = extractHelpFromReadme(await readme.text());
90-
const usageText = [...generateMarkdown(splitLines(rawUsageText))].join("\n");
72+
export async function generate(readmeContents: string) {
73+
const rawHelpText = extractHelpFromReadme(readmeContents);
74+
const usageText = [...generateMarkdown(splitLines(rawHelpText))].join("\n");
9175

9276
assert(
9377
usageText.match("\n## Options\n"),
@@ -108,37 +92,3 @@ export async function generateCliOptionsMarkdown() {
10892

10993
return usageText;
11094
}
111-
112-
export function generateCliOptionsIntegration(
113-
templatePath: string,
114-
): AstroIntegration {
115-
const [dir, file] = [dirname(templatePath), basename(templatePath)];
116-
117-
const outputPath = join(dir, file.replace("_", ""));
118-
119-
return {
120-
name: "lycheeverse:generate-cli-page",
121-
hooks: {
122-
"astro:config:setup": async ({ logger, addWatchFile }) => {
123-
logger.info(`Using template file ${templatePath}`);
124-
125-
addWatchFile(realpathSync(templatePath));
126-
addWatchFile(import.meta.filename);
127-
128-
logger.info(`Fetching from git tag ${LYCHEE_VERSION}`);
129-
rmSync(outputPath, { force: true });
130-
const usageText = generateCliOptionsMarkdown();
131-
132-
const docTemplateText = readFileSync(templatePath, "utf-8");
133-
const docOutput = docTemplateText.replace(TEMPLATE, await usageText);
134-
135-
assert(
136-
docOutput !== docTemplateText,
137-
`Placeholder ${TEMPLATE} not found in template file`,
138-
);
139-
logger.info(`Writing output file ${outputPath}`);
140-
writeFileSync(outputPath, docOutput);
141-
},
142-
},
143-
};
144-
}

0 commit comments

Comments
 (0)