22 lines
778 B
JavaScript
22 lines
778 B
JavaScript
// Exact source ranges, following internal/pipeline/definition.go's block rules.
|
|
// Never reserialize graph fields: defaults and authored spelling differ.
|
|
export function stageSource(source, key) {
|
|
if (typeof source !== "string") return null;
|
|
let active = false;
|
|
let start = null;
|
|
let offset = 0;
|
|
for (const line of source.match(/[^\n]*\n|[^\n]+$/g) ?? []) {
|
|
const trim = line.trim();
|
|
if (active) {
|
|
if (trim === "```") active = false;
|
|
} else if (trim.startsWith("# stage ")) {
|
|
if (start !== null) return source.slice(start, offset);
|
|
if (trim.slice(8).trim() === key) start = offset;
|
|
} else if (trim.startsWith("```")) {
|
|
active = true;
|
|
}
|
|
offset += line.length;
|
|
}
|
|
return start === null ? null : source.slice(start);
|
|
}
|