/** * @fileoverview Client-side section extraction from a NoteJson body for * `format: 'section'`. Headings are found the way the plugin's markdown-patch * finds them for its document map and PATCH targeting, so a section read * covers the span a section write edits, sliced from the note's own bytes. * `marked` is pinned exact to the 17.x line markdown-patch parses with: * `marked` 18 reads some shapes differently (a `#tag` line over `===`, a tab * before an ATX closing sequence, an empty `- ` item), which would split this * scan from the plugin's document map. * @module services/obsidian/section-extractor */ import type { Within } from './patch-instruction.js'; import type { NoteJson, SectionTarget } from './types.js'; /** An extracted section value plus, for heading targets, how the locator resolved. */ export interface SectionExtraction { /** * Every full heading path the target could name, in document order: each * heading sharing a bare-leaf target, or each repeat of a delimited target's * resolved path. Present only when more than one heading matched — the read * still returns the first, so this is disclosure, not a new failure mode. */ candidates?: string[]; /** * Full `::`-joined heading path, root included, that the read resolved to. * Heading sections only. */ sectionTarget?: string; /** Raw markdown (heading/block) or the JSON-typed frontmatter value. */ value: unknown; } /** * Extract a section's raw markdown (heading/block) or JSON value (frontmatter). * Throws `NotFound` if the target does not exist in the note. */ export declare function extractSection(note: NoteJson, section: SectionTarget): SectionExtraction; /** A heading as the plugin's parser reads it, located in the note's own bytes. */ export interface Heading { /** * Offset just past the heading's last line — its underline, for a setext * heading — and that line's ending, or the note's length. */ end: number; /** 1–6: the `#` count of an ATX heading, 1 or 2 for a setext `=` or `-` underline. */ level: number; /** Offset of the heading's first byte: its `#` line (indent included), or a setext heading's first text line. */ start: number; /** * Heading text as the document map keys it: the parser's text, trimmed. * Empty for an untitled heading; a multi-line setext heading keeps its * newlines. */ text: string; } /** * Every heading in `content`, in document order, found the way the plugin's * markdown-patch 2.0 finds them for its document map and PATCH targeting: the * frontmatter-stripped body, line endings normalized, lexed with `marked`, and * each top-level `heading` token located by accumulating token `raw` lengths. * That reads setext headings and skips `#` lines inside fences, HTML blocks, * list items, and blockquotes, exactly as the map does. Offsets are mapped * back to `content`, so a slice between them is the note's own bytes, CRs * included. */ export declare function scanHeadings(content: string): Heading[]; /** A section's own body, as markdown-patch 2.0 models it for a `within` write. */ export interface SectionBody { /** * `marked` token type of each top-level block in the section's direct body — * below its heading line and above its first sub-heading — in order: the * blocks a 2.0 `within` index counts (`paragraph`, `list`, `table`, `code`, …). */ blocks: string[]; /** * The section's `content` scope in the note's own bytes: below its heading * line through its last subsection. What 2.0's `rejectIfContentPreexists` * searches on a plain heading write. */ content: string; /** Whether sub-headings follow the direct body — a plain append lands below them. */ subsections: boolean; } /** * The body of the heading at full path `path` (its first occurrence), read * from one lex of the note, or `undefined` when the note has no such heading. * 2.0 assigns a section every top-level token between its heading and the * next heading of any level; a `space` token is not a block. */ export declare function sectionBody(content: string, path: string): SectionBody | undefined; /** * The span markdown-patch 1.x edits for the heading at full path `path` (its * first occurrence; the service refuses a write to a repeated path), as * offsets into `content`, or `undefined` when the note has no such heading. * `start` is where a prepend or replace splices in: one past the heading's raw * text with trailing whitespace trimmed, which is just past an ordinary * heading line's line ending, inside a line's trailing spaces, and past the * end of a note whose unterminated last line is the heading. `end` is where an * append splices in: the next heading at the same or a shallower level, or the * end of the note. * * The 1.x engine lexes the frontmatter-stripped body with its line endings * normalized, then applies the token offsets to the note's own bytes unmapped; * these offsets are computed the same way, so on a CRLF note they drift as the * engine's do. The engine's offsets sum only the tokens `marked` keeps, so * below a definition it drops they fall short of these; they agree wherever a * write goes out, since a write whose span such a definition shifts is * refused first (`droppedDefinitionInReach`). */ export declare function v1HeadingSpan(content: string, path: string): { end: number; start: number; } | undefined; /** The `marked` token type of each top-level block in `markdown`, in order. */ export declare function blockKinds(markdown: string): string[]; /** A link reference definition the plugin's `marked` drops, as a refused write names it. */ export interface DroppedDefinition { /** The label as the definition writes it, between its brackets. */ label: string; /** 1-based line of `content` the definition starts on, counting `\r\n`, `\r`, and `\n` breaks. */ line: number; } /** A heading write, as `droppedDefinitionInReach` judges it. */ interface HeadingWrite { /** Whether the write creates the heading when the note lacks it (`createTargetIfMissing`). */ create: boolean; /** The heading's full `::`-joined path. */ path: string; /** The direct-body block a 2.0 `within` write addresses; absent for a plain write. */ within?: Within | undefined; } /** * The first top-level link reference definition the plugin's `marked` drops * — one repeating a label already defined, compared as `marked` compares * them: case-folded, whitespace runs collapsed — above the point a heading * write to full path `path` reads its offsets to, or `undefined` when none * does. Both markdown-patch engines place headings by summing the raw lengths * of the tokens `marked` keeps, so every offset below a dropped definition * falls short by its length; a write that reads one lands outside its * section. The point is, for: * * - a heading the note has (its first occurrence): the heading closing its * section, or the heading itself when the section runs to the end of the * note, whose end the engines take from the note's length; * - a 2.0 `within` write: the start of the direct-body block it addresses; * - a heading the note lacks, with `create`: the heading closing the section * of its deepest existing ancestor, where the engines insert it — none * when that section runs to the end of the note or no ancestor exists, since * the engines then insert at the note's end. A path through a repeated * heading resolves to a different ancestor in each engine (2.0 walks each * heading's first occurrence, 1.x takes the last occurrence of the longest * path the note has), so the point is the farther of their two; * - a heading the note lacks, without `create`: none, since no write goes in. * * Exact for the range each write reads: a prepend places by the section's * start and an append by its end, but each one's duplicate check and * empty-section test read the whole section, so both are judged by it. */ export declare function droppedDefinitionInReach(content: string, write: HeadingWrite): DroppedDefinition | undefined; /** * Full `::`-joined path of every heading in `content`, in document order, * repeats included — the markdown-patch 2.0 map's heading tree, flattened. * Deduplicated and with the `""` entry dropped, it is the 1.x map's `headings` * array. */ export declare function listHeadingPaths(content: string): string[]; /** * The level of the heading at full path `path` in `content` — its first * occurrence. For a path the note does not have: with `create`, the level * markdown-patch 2.0 gives it when `createTargetIfMissing` creates it — one * below the deepest existing ancestor for each missing segment, counted from 0 * when no ancestor exists — and without, `undefined`. */ export declare function sectionLevel(content: string, path: string, create: boolean): number | undefined; /** * The deepest proper ancestor of heading path `path` that `paths` holds — a * prefix ending where `path.split(HEADING_DELIMITER)` cuts it — as the index * of its first occurrence and the count of `path`'s segments below it, or * `undefined` when `paths` holds none. Each of `paths` is compared at most * once, at the one cut its length matches, so the search is linear in their * length and in `path`'s. */ export declare function deepestAncestor(paths: readonly string[], path: string): { index: number; missing: number; } | undefined; /** * True when `blockId` is referenced in `content` by an isolated marker: a * `^blockId` line on its own, after a blank line (or at the body's start), so * the parser reads it as its own paragraph and the id names the block above. * A marker line with no blank line above it continues that block instead (a * table row, a paragraph line, a list item), and a line inside a fence or * indented as code is not a marker. */ export declare function isIsolatedBlockId(content: string, blockId: string): boolean; /** The `#` run opening a top-level ATX heading in a markdown fragment. */ export interface AtxHeadingMarker { /** Length of the `#` run — the heading's level. */ hashes: number; /** The heading's line, trimmed, for messages. */ line: string; /** Offset of the run's first `#` in the fragment's `normalized` text. */ start: number; } /** A markdown fragment with its line endings read as `\n`, and its ATX heading markers. */ export interface AtxHeadingFragment { markers: AtxHeadingMarker[]; normalized: string; } /** * The `#` run of every top-level ATX heading in `markdown`, found as * markdown-patch 2.0 finds the headings it re-levels in written content: line * endings normalized, lexed with `marked`, and each top-level `heading` token * whose raw text opens with a `#` run. A setext heading, a `#` line inside a * fence or HTML block, and a hashtag carry none. */ export declare function atxHeadingMarkers(markdown: string): AtxHeadingFragment; /** * Mark line indices that fall inside fenced code blocks (` ``` ` or `~~~`) so * downstream scanning doesn't false-match on markdown-about-markdown notes. * The closer must match the opener's fence char, have at least as many fence * chars, and carry no info string (per CommonMark). An unclosed fence extends * to EOF. Pass `from` to skip a leading frontmatter block. */ export declare function computeFenceMask(lines: string[], from?: number): boolean[]; export {}; //# sourceMappingURL=section-extractor.d.ts.map