mdrecord

package
v0.14.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mdrecord is the Markdown machinery a RECORD BODY is read and written through: which lines are live markdown and which are not, where a section starts and stops, what a top-level bullet is, and where a trailing run of link-reference definitions ends.

It is a leaf for the reason core/grounds and core/issueschema are. Two record families carry the same constructs — the intent record's scope conditions and audit notes, the issue ledger's grounds — and a body reader spelled twice is a reader the two can disagree about, which is how a bullet one writer appends is a bullet the other cannot find. It imports the standard library and termsafe, itself a standard-library leaf, for the tree's one code-span pairer: no filesystem, no transport, no record store, and no notion of which family a body belongs to.

What it does NOT own is any heading's meaning. A caller supplies the heading pattern it is looking for; this package only says whether a line matching it is live, and which lines lie under it.

Index

Constants

View Source
const (
	MaskFence uint8 = 1 << iota
	MaskComment
)

Mask flags: why a line is not live markdown. Both are read the same way — the line is neither matched nor written — but they are reported separately, so a refusal names the thing the reader has to go and look at.

Variables

This section is empty.

Functions

func AnyMasked

func AnyMasked(mask []uint8, start, end int, flag uint8) bool

AnyMasked reports whether any line of [start, end) carries the given flag.

func CodeSpanRanges

func CodeSpanRanges(ln string) [][2]int

CodeSpanRanges returns the byte ranges of the inline code spans on one line, each paired by termsafe.PairCodeSpan (CommonMark: a run of backticks closed by the next run of exactly the same length).

func CountHeadings

func CountHeadings(lines []string, mask []uint8, headRe *regexp.Regexp) int

CountHeadings counts the live headings matching headRe.

func FencedUnderEveryRule added in v0.11.0

func FencedUnderEveryRule(lines []string) []bool

FencedUnderEveryRule reports, per line, whether EVERY reading reads that line as inside a fence (its delimiters included): TopLevel, ListNested, and the list-item reading of a run indented one to three columns. It is the reading for a reader that must never hide live text behind a disagreement between the rules — a gate whose missed finding is silent, a floor whose missed heading travels — so a line either rule reads as live is live to it. HTML comments are not consulted: what a comment hides is the caller's question, and the readers that ask this one read commented text.

func FirstContent added in v0.11.0

func FirstContent(lines []string) (line, col int)

FirstContent is the tree's one leading-comment locator: the first line holding anything but blanks and HTML comments, and the byte column in that line where the content starts. A byte-order mark on line 0 is not content. Comments are read as Read reads them: the first `-->` closes one, a line may hold several, and content after a closer is content. It returns (len(lines), 0) when there is nothing else — a document of blanks and comments, or one whose comment never closes.

Four readers once skipped a leading attribution comment each by its own walk on HasPrefix and Contains, and each read a closer followed by text, or two comments on one line, its own way (iss-2609251517210637).

func InAnyRange

func InAnyRange(ranges [][2]int, pos int) bool

InAnyRange reports whether pos falls inside one of the ranges.

func IsHeading

func IsHeading(ln string) bool

IsHeading reports whether a line is a markdown ATX heading. It judges the line alone: whether that heading is LIVE is Mask's answer, not this one's.

func IsTopLevelBullet

func IsTopLevelBullet(ln string) bool

IsTopLevelBullet reports whether a line opens a column-0 list item.

func LabelledParagraph added in v0.11.1

func LabelledParagraph(lines []string, label string) (start, end int, ok bool)

LabelledParagraph returns the [start, end) line bounds of the first live paragraph whose first line opens with the bold label `**<label>.**`, and whether one exists. The paragraph runs to the next blank line, the next live heading, the next masked line, or the end of the file, whichever is first.

It is the one notion of a labelled paragraph, shared by the record lint that judges a principle's statement and the reading assembler that projects it (spc-2609020626042471): a principle carries one heading, its H1, and a body of labelled paragraphs (`**The rule.**`, `**Why.**`, `**Bounds.**`), so its statement is found by label rather than by heading, and two readers of that label that disagree are a gate refusing a paragraph the projection never sends, or the reverse.

A label inside a fence or an HTML comment is an example, not a statement, and is never matched. The label must open the line: a paragraph that mentions `**The rule.**` part-way through is not the rule.

func Mask

func Mask(lines []string) []uint8

Mask reports, per line, whether that line lies inside a fenced code block or an HTML comment span — the delimiter lines included, since nothing on them is live markdown either. A parser that cannot see either reads an EXAMPLE, or a deliberately PARKED bullet, as an instruction: a fenced or commented `## Scope Conditions` shadows the record's real one, and a bullet inside either is counted as a condition and written into by the stamp. Writing a marker inside a comment is the worst of the three — the marker's own `-->` closes the comment early, so the rest of the parked block starts rendering (iss-2608300235388164, iss-2608300259316871).

Neither construct nests in the other: inside a fence `<!--` is literal text, and inside a comment a fence delimiter is. An unclosed opener of either kind runs to end of file — CommonMark's rule for a fence, and the only safe reading of a comment nobody closed.

Mask is Read under the TopLevel rule.

func MaskCodeSpans added in v0.14.0

func MaskCodeSpans(s string) (masked string, unmask func(masked string, rendered bool) string)

MaskCodeSpans hides each inline code span in s behind an opaque token, so a rewrite of the text around the spans (a link read as its text, an HTML tag dropped, a character reference decoded) cannot reach into one, where CommonMark reads every character literally. Spans pair by termsafe.PairCodeSpan, so an unpaired run is literal backticks, and a backtick behind a backslash opens nothing (a backslash behind one does). A token is a NUL, the span's ordinal and a NUL, which no markdown construct matches; a string already holding a NUL is left unmasked.

unmask puts each span back into a rewritten string, as written or, with rendered, as the text it renders (termsafe.CodeSpanText); a token the rewrite dropped stays dropped.

func OpensComment

func OpensComment(ln string) bool

OpensComment reports whether a line leaves an HTML comment open. It walks the line left to right and lets the FIRST construct win, which is CommonMark's own precedence: at a backtick run it skips a matched code span whole (or treats an unmatched run as literal backticks), and at `<!--` it skips to the `-->` that closes it — consuming any backticks in between, because inside a comment they are literal text.

Resolving code spans first, as this did, diverged in both directions: a backtick inside a LIVE comment re-paired the rest of the line, and a comment opener quoted in backticks read as live. One cursor closes both (iss-2608300320418618, iss-2608300335369473). A well-formed identity marker closes on its own line, so it never opens a span; a genuinely unclosed opener masks to end of file.

func OpensFence added in v0.11.1

func OpensFence(ln string, rule Rule) bool

OpensFence reports whether a line, read outside any open fence or comment, opens a fenced code block under a rule. It is the opener Read applies, exported for a reader that judges one line rather than walking a body — the site renderer asking whether the block the walk handed it opens with a fence — so that reader takes the walk's rule instead of a prefix test of its own, which read a balanced span such as three backticks, x, three backticks as a fence (iss-2609262309556167).

func PeelTrailingLinkRefs

func PeelTrailingLinkRefs(section *[]string) []string

PeelTrailingLinkRefs removes a trailing run of markdown link-reference definitions (and any blank lines interspersed with them) from *section and returns that run, so an appending writer can re-emit it BELOW the new block. The run must contain at least one real definition — a tail of pure blank lines is not refs and is left to the caller's blank-trimming. Both the newly exposed end of *section and the leading blanks of the returned run are trimmed, so the caller reinstates exactly one separator on each side.

func SectionLineRange

func SectionLineRange(lines []string, headRe *regexp.Regexp) (start, end int, ok bool)

SectionLineRange returns the [start, end) line bounds of the body of the first live section whose heading matches headRe, and whether such a heading exists, which is what separates an absent section from an empty one.

This is the single notion of where a section starts and stops, so every reader and every writer of a record body agrees about which lines belong to which section.

func SectionLineRangeIn

func SectionLineRangeIn(lines []string, mask []uint8, headRe *regexp.Regexp) (start, end int, ok bool)

SectionLineRangeIn is SectionLineRange over a mask the caller already computed. A masked line — fenced or commented — is neither the heading that opens a section nor the heading that closes one.

func TrimBulletPrefix

func TrimBulletPrefix(ln string) string

TrimBulletPrefix strips a top-level list marker, leaving the bullet's prose.

func Unclosed

func Unclosed(lines []string) (line int, flag uint8, ok bool)

Unclosed reports the line that opened a span nothing closes before the end of the input, and which construct it is (MaskFence or MaskComment).

An unclosed opener is not itself a fault — running it to end of file is CommonMark's rule for a fence and the only safe reading of a comment nobody closed, and Mask does exactly that. What it IS, is the reason every line below it stops being live markdown, so a writer whose appended line lands there can never read it back. That writer has to be able to NAME the line, or its refusal describes the symptom and sends the operator to rewrite whatever it was appending — which is the one part of the record that is innocent (iss-2608301803423101).

Unclosed is Read under the TopLevel rule.

func UnwrapLinkPass added in v0.14.0

func UnwrapLinkPass(s string, shortcuts bool) (string, int)

UnwrapLinkPass makes one left-to-right pass over a line of prose, a heading title or a principle's statement, and replaces each outermost link or image it can read with its label, following the CommonMark inline link grammar (spec 6.3 and 6.4) closely enough for either:

  • the label runs to its matching `]`, with brackets balanced and a backslash escape read as a literal character (matchBrackets);
  • the inline form's destination is `<...>`, on one line with `\>` escaped, or a run of non-space bytes whose unescaped parentheses balance, and an optional title follows in `"..."`, `'...'` or `(...)` (inlineTail);
  • the full and collapsed reference forms, `[label][ref]` and `[label][]`, drop the second bracket pair. Whether a definition exists is not asked;
  • the shortcut `[label]` drops its brackets when shortcuts is true, the reading floor's choice: a bracketed title with no definition renders with its brackets, and reading it as its label too redacts it, which is the side a floor errs on. When shortcuts is false a shortcut is left as written, the principle statement's choice, since without its definition it is literal text; the links inside its brackets are still unwrapped;
  • an image, `!` before any of these, is unwrapped to its alt text: the heading a screen reader announces and a page without images shows.

A label followed by an inline tail the scanner cannot read is left as it is written, brackets and all, rather than read as a shortcut. Reading it as a shortcut would take its brackets away and hide from the reading floor's backstop a link the scanner failed to read.

The scanner was the reading floor's alone, and the principle statement's unwrap kept a pattern that ended a destination at its first `)`, so `[label](https://x/Audit_(finance))` left `)` and the address's tail in the projected statement. Both read through this one pass now.

The pass is linear in the title by construction. The bracket table is one stack walk; the main walk visits each byte once, jumping past what it unwraps; and the only re-reading is an inline tail that fails, which is charged to a budget of the title's own length. Once the budget is spent the rest of the title is copied as written, so its brackets stay for the backstop. The work done, in bytes, is returned for the test that holds the bound.

Types

type BulletBlock

type BulletBlock struct{ Start, End int }

BulletBlock is one top-level bullet and the continuation lines folded into it, as the half-open line range [Start, End).

func BulletBlocks

func BulletBlocks(lines []string, mask []uint8, start, end int) []BulletBlock

BulletBlocks splits a section body into its top-level bullets. It is the single notion of where an item starts and stops, so a reader and a writer can never disagree about which lines belong to which item. A masked line — fenced or commented — neither opens a bullet nor continues one.

type Reading added in v0.11.0

type Reading struct {
	Mask   []uint8
	Fences []Span
	// contains filtered or unexported fields
}

Reading is one pass over a body under one Rule: the per-line mask, every fenced block as its own span (a per-line mask cannot say where one fence ends and the next, written directly below it, begins), and the span still open at the end, if any.

func Read added in v0.11.0

func Read(lines []string, rule Rule) Reading

Read is the single pass every reading of fences and comments goes through. Spelled twice, two readers could disagree about which lines a span covers and which line opened it.

func (Reading) Unclosed added in v0.11.0

func (r Reading) Unclosed() (line int, flag uint8, ok bool)

Unclosed reports the line that opened the span still open at the end of the input, and which construct it is — see the package-level Unclosed.

type Rule added in v0.11.0

type Rule uint8

Rule is which fences a reading recognises. This package is the tree's one notion of a fence and an HTML comment; the rule is the one place a reader may ask for more than CommonMark's top level, and it may ask for exactly one thing more.

const (
	// TopLevel is CommonMark's fence rule at the top level of a document: a run
	// of three or more backticks or tildes at up to three spaces of indent,
	// closed by a run of the same character at least as long, at up to three
	// spaces of indent, with nothing after it. A backtick opener's info string
	// may not itself hold a backtick.
	TopLevel Rule = iota
	// ListNested is TopLevel plus a fence indented under a list item. The site
	// renders fences written inside list items, where the fence sits at the
	// item's content indent and that may be four columns or more; a reader that
	// saw only the left margin would read the fence's own `#` lines as headings.
	//
	// A run at four or more columns cannot open a fence at the top level (it is
	// indented code there), so under ListNested it opens a fence in the list
	// item holding it. That fence closes on a run of the opener's character at
	// least as long, with nothing after it, indented no more than three columns
	// past the opener (further in is content: CommonMark's closer allowance,
	// measured from the item). And because fenced code never continues lazily,
	// a non-blank line at column 0 ends the item and the fence with it
	// (CommonMark 5.2). A run at up to three columns is read exactly as TopLevel
	// reads it, so the two rules differ only where TopLevel sees no fence at all.
	ListNested
)

type Span added in v0.11.0

type Span struct {
	Start, End int
	Closed     bool
}

Span is one fenced block as the half-open line range [Start, End), its delimiter lines included. Closed is false when the fence runs to the end of the input.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL