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 only the standard library: 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 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 Mask(lines []string) []uint8
- func OpensComment(ln string) 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 TrimBulletPrefix(ln string) string
- func Unclosed(lines []string) (line int, flag uint8, ok bool)
- type BulletBlock
- 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 CodeSpanRanges ¶
CodeSpanRanges returns the byte ranges of the inline code spans on one line: a run of backticks closed by a run of the same length (CommonMark).
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 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 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.
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 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.
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
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.
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 )