/** * @fileoverview The two markdown-patch wire formats a section write and a * document-map read speak, and the one negotiation rule between them. Local * REST API 5.x and later speak markdown-patch 2.0 — a JSON instruction body * with an array heading target, and a nested document map. Plugin 4.x speaks * only 1.x — `Operation` / `Target-Type` / `Target` request headers and a flat * `::`-joined map. Plugin 6.0 removes 1.x. * @module services/obsidian/patch-instruction */ import { type AtxHeadingFragment } from './section-extractor.js'; import type { DocumentMap, PatchInstruction } from './types.js'; /** A markdown-patch format, spelled as its `Markdown-Patch-Version` header value. */ export type PatchFormat = '1' | '2'; /** * The markdown-patch format a plugin at `version` (`GET /`'s `versions.self`) * speaks. A version whose major does not parse reads as 2.0, the only format * plugin 6.0 and later accept. */ export declare function patchFormatFor(version: string | undefined): PatchFormat; /** * Request headers for a 1.x header-driven PATCH. `Markdown-Patch-Version: 1` * is what plugin 5.x needs to accept header targeting; plugin 4.x predates the * header and ignores it. */ export declare function v1PatchHeaders(p: PatchInstruction): Record; /** * The body of a 1.x markdown heading write to `note`. The 1.x engine splices * content in exactly as given, so content without a line break on a side runs * into the line it meets there (`- three## B`). A line break is added after * non-empty content that lacks one, and before content that lacks one where * the note text the engine splices after does not end in one: an append to a * section that runs to the end of a note with no final line break, any write * to an empty section whose heading is the note's unterminated last line, an * append with `trimTargetWhitespace` (which trims that line break away), and a * prepend or replace under a heading line with trailing whitespace (which the * engine splices inside). Empty content stays empty — a `replace` with it * clears the section. A section the note lacks is created on its own line, * so content for it needs only the closing break. * * A CRLF note stays out of reach: the engine places offsets counted on its * normalized line endings into the note's own bytes, so the write lands * mid-line whatever the content. */ export declare function v1HeadingBody(p: PatchInstruction, content: string, note: string): string; /** A markdown-patch 2.0 write instruction, as this server sends one. */ export interface V2Instruction { content?: string; createTargetIfMissing?: true; operation: PatchInstruction['operation']; rejectIfContentPreexists?: true; scope?: 'content'; target: string | string[]; targetType: PatchInstruction['targetType']; value?: unknown; within?: Within; } /** * A heading's direct-body block, by position: `0` the first, `-1` the last. * markdown-patch 2.0 resolves it afresh on every write; nothing is written to * the note to address it. */ export type Within = 0 | -1; /** * The markdown-patch 2.0 instruction for a section write. `content` is the * payload as it should reach the engine: for a heading target, already carrying * section-relative heading levels (see `relativeHeadingLevels`). * * The payload rides in the carrier the target reads. Frontmatter takes a JSON * `value` — the parsed literal under `contentType: "json"`, the text itself * otherwise, which is what the 1.x format made of a markdown body. A block * under `json` takes table rows as a `value`; a single row is wrapped, as 1.x * accepted it bare. Everything else is literal `content`. (Rows under a * heading have no 2.0 form and go out as 1.x — see the service's * `#wireFormat`.) * * With `within`, a heading append or prepend addresses that one body block at * `content` scope instead of the section, and 2.0 splices the content at the * block's edge exactly as given — no blank line, no heading re-levelling. The * line break on the side facing the block is added here, so `content` is the * reduced text of `canonicalContent`. 2.0 refuses `createTargetIfMissing` * beside `within`, which addresses a block that already exists. * * There is no `trimTargetWhitespace`: 2.0 owns the whitespace around inserted * content, and has no such field. */ export declare function v2Instruction(p: PatchInstruction, content: string, within?: Within): V2Instruction; /** * `content` reduced as markdown-patch 2.0 reduces a plain heading write before * placing it: line endings read as `\n`, leading blank lines and all trailing * whitespace dropped, first-line indentation kept (`canonicalFragment` in * markdown-patch 2.0.0, less the one line ending it then adds). The trailing * trim is `trimEnd()`, which drops exactly what 2.0's `/\s+$/` does; on Node * that regex backtracks across every whitespace run that does not end the * content, in time quadratic in the run. */ export declare function canonicalContent(content: string): string; /** Heading content rewritten to 2.0's section-relative levels, or the heading that cannot be. */ export type RelativeLevels = { ok: true; content: string; } | { ok: false; heading: string; level: number; }; /** * Rewrite heading content from the absolute `#` levels a caller writes to the * relative ones markdown-patch 2.0 reads. 2.0 adds the target section's level * (`baseline`) to every top-level ATX heading in written content, where 1.x * wrote it as given; subtracting it first lands the levels the caller wrote. * * A heading at or above `baseline` has no relative form — 2.0 keeps written * content inside its section, and such a heading would close it — so it is * returned instead. Line endings come back as `\n`, which 2.0 normalizes to * the note's own anyway. */ export declare function relativeHeadingLevels(fragment: AtxHeadingFragment, baseline: number): RelativeLevels; /** A markdown-patch 2.0 heading tree: each key a heading, each value its child headings. */ export interface HeadingTree { [text: string]: HeadingTree; } /** The markdown-patch 2.0 document map, as plugin 5.x serves it. */ export interface RawDocumentMapV2 { blocks: string[]; frontmatterFields: string[]; headings: HeadingTree; version: string; } /** * Every heading in a markdown-patch 2.0 heading tree as its `::`-joined full * path, in tree order, one entry per occurrence. A top-level untitled heading * is `""`, and its children read `::Child`, as the 1.x map writes them. */ export declare function flattenHeadingTree(tree: HeadingTree, parent?: string): string[]; /** * The 2.0 document map in the flat 1.x shape this server's document map has * always carried. The 2.0 map keeps an entry per repeated heading and block id; * the 1.x map keyed both by name, so repeats collapse to the first and a * top-level untitled heading's own `""` entry drops, as it did there. * * Blocks and frontmatter fields take the order the 1.x map gave them: it * listed each as the keys of a plain object, so JavaScript's key order * applied — integer-like names first, ascending. Headings keep the tree's * order, which is the note's unless a heading name is integer-like (see * `hasIntegerKey`); the service restores note order in that case. */ export declare function flattenDocumentMap(map: RawDocumentMapV2): DocumentMap; /** * True when `name` is an integer-like object key — a canonical array index — * which JavaScript lists ahead of every other key. A heading named `2025` is * one, so a map built from object keys (the 2.0 tree, and the 1.x map's keys) * lists it out of note order. */ export declare function isIntegerKey(name: string): boolean; /** True when any heading in the tree, at any depth, is named with an integer-like key. */ export declare function hasIntegerKey(tree: HeadingTree): boolean; /** * `paths` reordered to the note: by where each first occurs in `notePaths` * (the note's heading paths in document order), ties and unknown paths keeping * their relative order. */ export declare function inNoteOrder(paths: readonly string[], notePaths: readonly string[]): string[]; //# sourceMappingURL=patch-instruction.d.ts.map