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
- func AnyMasked(mask []uint8, start, end int, flag uint8) bool
- func BlankLine(ln string) bool
- func CodeSpanRanges(ln string) [][2]int
- func CountHeadings(lines []string, mask []uint8, headRe *regexp.Regexp) int
- func FencedUnderEveryRule(lines []string) []bool
- func FirstContent(lines []string) (line, col int)
- func InAnyRange(ranges [][2]int, pos int) bool
- func IsHeading(ln string) bool
- func IsTopLevelBullet(ln string) bool
- func LabelledParagraph(lines []string, label string) (start, end int, ok bool)
- func Mask(lines []string) []uint8
- func MaskCodeSpans(s string) (masked string, unmask func(masked string, rendered bool) string)
- func OpensComment(ln string) bool
- func OpensFence(ln string, rule Rule) bool
- func PeelTrailingLinkRefs(section *[]string) []string
- func SectionLineRange(lines []string, headRe *regexp.Regexp) (start, end int, ok bool)
- func SectionLineRangeIn(lines []string, mask []uint8, headRe *regexp.Regexp) (start, end int, ok bool)
- func SectionLineRangeNestedIn(lines []string, mask []uint8, headRe *regexp.Regexp) (start, end int, ok bool)
- func SureCodeSpans(lines []string, skip []bool) [][][2]int
- func TrimBulletPrefix(ln string) string
- func Unclosed(lines []string) (line int, flag uint8, ok bool)
- func UnwrapLinkPass(s string, shortcuts bool) (string, int)
- type BulletBlock
- type Page
- type Reading
- type Rule
- type Span
Constants ¶
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 BlankLine ¶
BlankLine reports whether a line is blank as CommonMark reads it: empty or nothing but spaces and tabs, before an optional trailing `\r`. Every test of where a paragraph or block ends goes through it, so a line holding only a no-break space, another Unicode space, a vertical tab or a form feed, which strings.TrimSpace would empty but the page reads as text, never ends one.
func CodeSpanRanges ¶
CodeSpanRanges returns the byte ranges of the inline code spans on one line, the line read alone: a run of backticks closed by the next run of exactly the same length (termsafe.PairCodeSpan's rule, through the linear termsafe.CodeSpanPairer), where a backtick behind an odd run of backslashes opens none, as on the page outside a span. The line's HTML comments are not consulted.
func CountHeadings ¶
CountHeadings counts the live headings matching headRe.
func FencedUnderEveryRule ¶ added in v0.11.0
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
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 ¶
InAnyRange reports whether pos falls inside one of the ranges.
func IsHeading ¶
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 ¶
IsTopLevelBullet reports whether a line opens a column-0 list item.
func LabelledParagraph ¶ added in v0.11.1
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 ¶
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
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 ¶
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.
The line is read alone, and an opener after an unmatched run opens a comment here, which is the answer a writer asking before it appends the line needs: on the page that run may pair with nothing. A backtick behind an odd run of backslashes opens no span (CodeSpanRanges), so the next backtick may open one that holds an opener.
func OpensFence ¶ added in v0.11.1
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 ¶
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 ¶
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 SectionLineRangeNestedIn ¶ added in v0.17.0
func SectionLineRangeNestedIn(lines []string, mask []uint8, headRe *regexp.Regexp) (start, end int, ok bool)
SectionLineRangeNestedIn is SectionLineRangeIn for a section that holds sub-sections of its own: its body runs to the next live heading of the same depth or shallower, indented up to three columns as its own may be, so a `### Relay` under `## Open Questions` is inside it and a `## Decisions` after it is not.
func SureCodeSpans ¶ added in v0.17.0
SureCodeSpans is CodeSpanRanges for each line of a body read top to bottom, for a reader that blanks code spans where it is sure of them and reads the rest whole: the docs lint's prose checks, which keep their own fence mask and read comment text as text. A line below a backtick run a line above it in its stretch leaves open, to the next blank line, reports no span, since the page may pair that run with a backtick on the line; the run's own line keeps its spans. A line skip marks (a fenced line the caller does not read) is not walked and reports none, and a blank one still ends the stretch, as in Read. HTML comments are not consulted.
func TrimBulletPrefix ¶
TrimBulletPrefix strips a top-level list marker, leaving the bullet's prose.
func Unclosed ¶
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
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 Page ¶ added in v0.17.0
type Page struct {
// Text is each line as a reader of the rendered page sees it: a line
// inside a fenced block, its delimiters included, is "", and every HTML
// comment span on a line (one that opens and closes there, the tail of one
// opened above, the head of one that closes below) is replaced by spaces of
// the same length, so the text before a comment's opener and after its
// closer is read where it sits, at its own offsets. From the line Unsure
// names on, every line is its own text, nothing blanked.
Text []string
// Mask is the per-line mask of the same reading, for a section's bounds:
// a line is masked only where Text does not read it.
Mask []uint8
// Spans is, per line, the byte ranges of Text inside an inline code span
// that opens and closes on the line, outside a comment, on a line that no
// line above it in its stretch leaves a backtick run open on (inline.go).
// A fenced line, a line below such a run to the next blank line, and
// every line from the one Unsure names, report none.
Spans [][][2]int
// contains filtered or unexported fields
}
Page is a body as a reader of what the page SAYS reads it (ReadPage).
func ReadPage ¶ added in v0.17.0
ReadPage is the one reading for a gate that refuses on what the text states: it reads every line its three readings do not all agree is fenced or commented. Its fences are ListNested's and its comments are Read's: the first raw `-->` closes a comment, a code span outside one is skipped whole (a `<!--` quoted in backticks opens nothing), and inside a fence a comment opener is literal. Mask leaves a comment that opens and closes on one line live, because a writer may touch that line; Text blanks it.
Every line is either returned as text or lies in a fenced block or an HTML comment that every reading here agrees on. This package does not parse lists, blockquotes or HTML blocks, so where the extent of a fence or a comment can turn on one of them the page is Unsure from the first such line, and that line and every line below it are returned as their own text. That first line is the first where:
- TopLevel, ListNested and the list-item reading mask the line differently, as each reads a fence indented four columns or more;
- a non-blank line inside a fence or a comment whose opener is indented and sits in a list item is indented less than the opener, or less than the item's content column;
- an HTML comment that runs past its line opens at four columns or more, or after other text on that line and is not closed inside the same paragraph (inParagraph; the opener's line is named);
- a list marker and a fence delimiter share a line;
- a fence delimiter sits in an HTML block (a line opening with a tag, a processing instruction or a CDATA section, after any list markers, to the next blank line, or to its closer: htmlBlocks), or a comment opener sits in a raw text element's block (rawTextTags);
- a comment opener sits behind a backslash;
- a comment opener's comment can be read another way (Reading.Unsure).
type Reading ¶ added in v0.11.0
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
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.
Each line outside a fence is walked alone (inline.go): a comment opener inside a one-line code span opens nothing, and one outside a span opens a comment. From a backtick run a line leaves open to the next blank line the page may pair that run over a line break, and where that turns which comments there are, the reading is Unsure from the line.
func (Reading) Unclosed ¶ added in v0.11.0
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.
func (Reading) Unsure ¶ added in v0.17.0
Unsure reports the first line holding a comment opener whose comment the page may read another way, and whether there is one: below a backtick run a line above it in its stretch left open, or after a run its own line leaves open, an opener that is not the first thing on its line, at up to three columns, and has no `-->` after it on the line, whether the walk met it or it sits inside a one-line span on a line below the run. The page may pair that run over the line break, so the opener may sit inside a span, or a span the walk paired may not be one. Mask reads the opener as a line read alone does, a comment opened outside a span; a writer refuses such a body, and ReadPage reads it whole from that line.
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 )