файл пакета→ .shtukbase/packages/layout-minimal/actions/adopt.ts
actions/adopt.ts
20 строк, 856 Б.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
- const PAGE = 'design/design.md';
- export function adopt(text: string): string {
- const header = /^(\uFEFF?---(\r?\n))([\s\S]*?)(\r?\n---(?=\r?\n|$))/.exec(text);
- if (!header) return text;
- const data = Bun.YAML.parse(header[3]) as { layout?: unknown } | null;
- if (data?.layout != null && (typeof data.layout !== 'string' || data.layout.trim())) return text;
- const field = /^layout[ \t]*:[^\r\n]*/m;
- const line = 'layout: layout-minimal';
- const yaml = field.test(header[3]) ? header[3].replace(field, line) : header[3] + header[2] + line;
- return header[1] + yaml + header[4] + text.slice(header[0].length);
- }
- if (import.meta.main && existsSync(PAGE)) {
- const text = readFileSync(PAGE, 'utf8');
- const next = adopt(text);
- if (next !== text) writeFileSync(PAGE, next);
- }