mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-16 23:16:26 +00:00
feat(cli): preserve custom sections after solution during docs sync
This commit is contained in:
+21
-1
@@ -359,6 +359,24 @@ function ensureLabel(page: string, h: Header): string {
|
|||||||
return page.replace(/^sidebar:$/m, `sidebar:\n${label}`);
|
return page.replace(/^sidebar:$/m, `sidebar:\n${label}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Offset just past the script-owned `## Solution` section: the next top-level
|
||||||
|
* heading that is not inside a code fence, or end of file. Sections a human
|
||||||
|
* appends after the solution (`## Explanation`, …) survive regeneration.
|
||||||
|
*/
|
||||||
|
function solutionEnd(page: string, from: number): number {
|
||||||
|
const lines = page.slice(from).split("\n");
|
||||||
|
let offset = from + lines[0]!.length + 1; // past the '## Solution' line itself
|
||||||
|
let fenced = false;
|
||||||
|
for (let i = 1; i < lines.length; i++) {
|
||||||
|
const line = lines[i]!;
|
||||||
|
if (line.startsWith("```")) fenced = !fenced;
|
||||||
|
else if (!fenced && line.startsWith("## ")) return offset;
|
||||||
|
offset += line.length + 1;
|
||||||
|
}
|
||||||
|
return page.length;
|
||||||
|
}
|
||||||
|
|
||||||
// Existing docs pages, keyed by leetcode number from the frontmatter title.
|
// Existing docs pages, keyed by leetcode number from the frontmatter title.
|
||||||
const pages = new Map<number, string>(); // num -> path relative to DOCS
|
const pages = new Map<number, string>(); // num -> path relative to DOCS
|
||||||
for (const rel of (await Array.fromAsync(new Bun.Glob("**/*.mdx").scan(DOCS))).sort()) {
|
for (const rel of (await Array.fromAsync(new Bun.Glob("**/*.mdx").scan(DOCS))).sort()) {
|
||||||
@@ -386,7 +404,9 @@ for (const sol of ordered) {
|
|||||||
report.push([workRef, existing, "ERROR: no '## Solution' heading"]);
|
report.push([workRef, existing, "ERROR: no '## Solution' heading"]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const next = ensureLabel(page.slice(0, at) + solutionSection(sol), sol.header);
|
const tail = page.slice(solutionEnd(page, at));
|
||||||
|
const body = page.slice(0, at) + solutionSection(sol) + (tail ? `\n${tail}` : "");
|
||||||
|
const next = ensureLabel(body, sol.header);
|
||||||
const actions: string[] = [];
|
const actions: string[] = [];
|
||||||
if (existing !== name) {
|
if (existing !== name) {
|
||||||
await rename(join(DOCS, existing), join(DOCS, name));
|
await rename(join(DOCS, existing), join(DOCS, name));
|
||||||
|
|||||||
Reference in New Issue
Block a user