ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/layout-minimal/actions/adopt.ts

actions/adopt.ts

20 líneas, 856 B.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. import { existsSync, readFileSync, writeFileSync } from 'node:fs';
  2. const PAGE = 'design/design.md';
  3. export function adopt(text: string): string {
  4. const header = /^(\uFEFF?---(\r?\n))([\s\S]*?)(\r?\n---(?=\r?\n|$))/.exec(text);
  5. if (!header) return text;
  6. const data = Bun.YAML.parse(header[3]) as { layout?: unknown } | null;
  7. if (data?.layout != null && (typeof data.layout !== 'string' || data.layout.trim())) return text;
  8. const field = /^layout[ \t]*:[^\r\n]*/m;
  9. const line = 'layout: layout-minimal';
  10. const yaml = field.test(header[3]) ? header[3].replace(field, line) : header[3] + header[2] + line;
  11. return header[1] + yaml + header[4] + text.slice(header[0].length);
  12. }
  13. if (import.meta.main && existsSync(PAGE)) {
  14. const text = readFileSync(PAGE, 'utf8');
  15. const next = adopt(text);
  16. if (next !== text) writeFileSync(PAGE, next);
  17. }