/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ const markdownMarkerPattern = /^$/s; const mdxMarkerPattern = /^\/\* markdown-magic:(begin(?: (\{.*\}))?|end) \*\/$/s; import type { RootContent } from "mdast"; import type { GeneratedRegion, ParsedDocument } from "./types.js"; type ParsedMarker = | { kind: "begin"; line: number; startOffset: number; endOffset: number; json: string } | { kind: "end" | "invalid-begin"; line: number; startOffset: number; endOffset: number }; /** * Creates a marker error with its source location. * * @param filePath - The document path. * @param line - The one-based source line. * @param message - The error details. * @returns An error that includes the source path and line. */ function markerError(filePath: string, line: number, message: string): Error { return new Error(`${filePath}:${line}: ${message}`); } /** * Parses a top-level syntax-tree node as a generated-region marker. * * @param node - The node to inspect. * @param format - The document format. * @returns The marker details, or `undefined` if the node is not a marker. */ function parseMarkerNode( node: RootContent, format: ParsedDocument["format"], ): ParsedMarker | undefined { const isMarkdownMarker = format === "markdown" && node.type === "html"; const isMdxMarker = format === "mdx" && node.type === "mdxFlowExpression"; if (!isMarkdownMarker && !isMdxMarker) { return undefined; } const pattern = format === "markdown" ? markdownMarkerPattern : mdxMarkerPattern; const match = pattern.exec(node.value); if (match === null) { return undefined; } const line = node.position?.start.line; const startOffset = node.position?.start.offset; const endOffset = node.position?.end.offset; if (line === undefined || startOffset === undefined || endOffset === undefined) { throw new Error("Generated marker nodes must contain source positions."); } if (match[1] === "end") { return { kind: "end", line, startOffset, endOffset }; } if (match[2] === undefined) { return { kind: "invalid-begin", line, startOffset, endOffset }; } return { kind: "begin", line, startOffset, endOffset, json: match[2] as string, }; } /** * Finds and validates generated regions in document order. * * Only top-level comment nodes can define regions. Generated regions cannot nest. * * @param document - The parsed destination document. * @returns The validated generated regions in source order. * @throws If a marker or marker pair is invalid. */ export function findGeneratedRegions(document: ParsedDocument): GeneratedRegion[] { const regions: GeneratedRegion[] = []; let openingMarker: | { transform: string; transformOptions: Record; line: number; endOffset: number; } | undefined; for (const node of document.tree.children) { const marker = parseMarkerNode(node, document.format); if (marker === undefined) { continue; } if (marker.kind === "invalid-begin") { throw markerError(document.path, marker.line, "Opening marker requires JSON options."); } if (marker.kind === "begin") { if (openingMarker !== undefined) { throw markerError(document.path, marker.line, "Generated regions must not nest."); } let options: unknown; try { options = JSON.parse(marker.json); } catch { throw markerError(document.path, marker.line, "Invalid marker JSON."); } if (options === null || Array.isArray(options) || typeof options !== "object") { throw markerError(document.path, marker.line, "Marker options must be a JSON object."); } const markerOptions = options as Record; if ( typeof markerOptions.transform !== "string" || markerOptions.transform.length === 0 ) { throw markerError( document.path, marker.line, 'Marker options require a non-empty "transform" string.', ); } const { transform, ...transformOptions } = markerOptions as Record & { transform: string; }; openingMarker = { transform, transformOptions, line: marker.line, endOffset: marker.endOffset, }; continue; } if (openingMarker === undefined) { throw markerError(document.path, marker.line, "Closing marker has no opening marker."); } regions.push({ destinationPath: document.path, destinationFormat: document.format, transformName: openingMarker.transform, options: openingMarker.transformOptions, openingMarkerEnd: openingMarker.endOffset, closingMarkerStart: marker.startOffset, line: openingMarker.line, }); openingMarker = undefined; } if (openingMarker !== undefined) { throw markerError(document.path, openingMarker.line, "Missing closing marker."); } return regions; }