import type { Node } from "comark"; import { defineComarkPlugin } from "comark"; import { errorText } from "../../../shared/text.ts"; import { OutsideRoot, diskReader, resolvePath } from "../../../shared/disk.ts"; /** Reads a file by its POSIX path relative to `root`, such as `guide/setup.md`. Returns undefined when it doesn't exist. */ export type IncludeReader = (path: string) => string | undefined | Promise; export interface IncludeOptions { /** The folder includes can't leave, and where a `src` that starts with `/` starts. Default `"."`, the working directory. */ root?: string; /** The host document's folder relative to `root`, where relative paths start. Default `""`, the root itself. */ base?: string; /** * Reads a file under `root`. Default reads from disk and refuses symlinks that lead out of `root`, so browsers and * edge runtimes pass their own. A custom reader only gets paths under `root` and must handle symlinks itself. */ read?: IncludeReader; /** How many levels of includes may nest. Default 8. */ depth?: number; } export type IncludeMeta = { /** Every file the document included or tried to include, relative to `root`, so a watcher can rebuild when one changes or appears. */ includes: string[]; /** One readable sentence per problem, naming its include and the fix. Other plugins add theirs too. */ issues: string[]; /** The comark-linter rule of each issue, by its sentence, so the linter reports it under its own code. */ issueRules: Record; }; type Issue = { rule: `comark/include/${string}`; message: string }; interface Run { read: IncludeReader; depth: number; includes: Set; issues: Issue[]; } const LINE = /^([ \t]*):include\{(.*)\}\s*$/; const ATTRIBUTE = /([^\s=]+)(?:=(?:"([^"]*)"|'([^']*)'|(\S+)))?/g; const FENCE = /^[ \t]*(`{3,}|~{3,})(.*)$/; const QUOTE = /^(?:[ \t]{0,3}>[ \t]?)+/; const ITEM = /^[ \t]*(?:[-*+]|\d{1,9}[.)])(?:[ \t]+|$)/; // Comark's block-name grammar, from comark/dist/internal/parse/syntax/block-params.js. const BLOCK_OPEN = /^[ \t]*(:{2,})([a-z$][$\w.-]*)/i; const BLOCK_CLOSE = /^[ \t]*(:{2,})\s*$/; // A marker starts its line, after a comment opener if it has one, so a string such as "#region a" isn't one. const REGION = /^[ \t]*(?:(?:\/\/|#|\/\*|