A fenced block is a quote only when the paragraph beside it pins it
Before a tool that checks quoted code can compare a single byte, it has to settle one question: which of an article's fenced blocks are claims about a repository. The obvious approach is to guess from the content, and it is the wrong one, because every shell transcript, config sketch and illustrative snippet would turn into a failed quote. The rule in srcproof is positional and explicit instead: a block counts as a claim only when the paragraph directly before or after it carries a commit-pinned file link with a line range.
I have written a lot of documentation for payment and crypto systems over the last decade, and the failure mode repeats: the snippet in the prose and the code in production part ways, and nobody notices because nothing checks. srcproof is my answer to that, a lockfile and a gate for prose that cites code. The comparison itself is dull. Slice the lines out of the blob at the pinned commit, compare byte for byte. What happens upstream of that is where the thinking went.
Why content cannot decide#
Suppose detection worked by inspecting the block. The info string is the first temptation: treat every block tagged go as a quote. An engineering article shows Go that is not from the repository all the time, a sketch of an API, a snippet of the standard library, the broken version of a function before the fix. Each of those becomes a false failure, and the author's only remedy is to lie about the language.
The second temptation is fuzzy search: look for the block's bytes somewhere in the tree and call it a quote if you find them. Now the verdict depends on how hard you looked. A three-line block of boilerplate matches in four files. A block that should match but has drifted by one space matches nowhere and is therefore silently not a quote, which is exactly backwards, because a drifted quote is the case the tool exists to catch.
srcproof already applies the opposite reasoning one level down. Its README prints the ref forms it refuses, a branch name, a tag, an abbreviated sha, an uppercase sha, because a tool that cannot document its own refusals is not much use. So links.Extract skips fenced blocks and code spans entirely: documentation may show a link it does not endorse. That courtesy has to extend upward too. A block that merely shows a permalink must not become a quote because of it, and ident.Spans skips fences for the same reason, so a Go name that a block only displays is not read as a claim about the code.
What is left is a rule the writer satisfies on purpose. Put a permalink with a line range in the paragraph that touches the block, and the block is a claim. Leave it out, and it is an illustration.
Where the tool looks#
quote.Blocks walks the article and finds fences first. fenceMarker accepts a run of at least three backticks or tildes indented by no more than three spaces, and a block closes only on a fence of the same character, at least as long, with nothing but whitespace after it; a fence that never closes yields no block at all. Every line from the opening fence to the closing one is recorded in inFence, which matters in a moment. Then the links of the article are bucketed by line and each block is offered its anchor:
byLine := make(map[int][]links.Link)
for _, link := range links.Extract(file, source) {
byLine[link.Line] = append(byLine[link.Line], link)
}
for i := range blocks {
if anchor, ok := anchorFor(blocks[i], lines, inFence, byLine); ok {
blocks[i].Anchor = &anchor
}
}anchorFor is the whole rule:
func anchorFor(block Block, lines []string, inFence []bool, byLine map[int][]links.Link) (Anchor, bool) {
paragraphs := [][]int{
paragraph(lines, inFence, block.Fence-1, -1),
paragraph(lines, inFence, block.Close-1, 1),
}
for _, numbers := range paragraphs {
for _, number := range numbers {
for _, link := range byLine[number] {
if anchor, ok := anchorOf(link); ok {
return anchor, true
}
}
}
}
return Anchor{}, false
}Two paragraphs, in a fixed order: the one before the opening fence, then the one after the closing fence. The second one is not a nicety, since people write "the code above comes from ..." about as often as they write "here is the function:", and resolving before-first keeps the answer stable when both paragraphs carry a link.
paragraph is where adjacency is made strict:
// paragraph returns the line numbers of the run of non-blank lines nearest a
// fence in one direction. Another fence ends the search: a permalink inside a
// code block is shown, not claimed.
func paragraph(lines []string, inFence []bool, fence, step int) []int {
i := fence + step
for ; i >= 0 && i < len(lines); i += step {
if inFence[i] {
return nil
}
if strings.TrimSpace(lines[i]) != "" {
break
}
}
var numbers []int
for ; i >= 0 && i < len(lines); i += step {
if inFence[i] || strings.TrimSpace(lines[i]) == "" {
break
}
numbers = append(numbers, i+1)
}
sort.Ints(numbers)
return numbers
}It steps away from the fence, skips blank lines, and collects the first run of non-blank lines it reaches. Two consequences are worth knowing if you write with this. A block whose neighbour is another block gets nothing from that side: the first non-blank line is inside a fence, so the search returns nil instead of reaching past it for a paragraph that belongs to somebody else. And a paragraph of prose sitting between the permalink and the block is not transparent, because the prose is the paragraph, the link is one paragraph further out, and the block ends up unanchored. Adjacency means adjacency.
What is allowed to anchor#
Finding a link near a block is not enough. anchorOf decides whether that link can carry a claim:
func anchorOf(link links.Link) (Anchor, bool) {
if link.Kind != links.KindBlob || !link.Pinned() {
return Anchor{}, false
}
marker := "/blob/" + link.Ref + "/"
at := strings.Index(link.URL, marker)
if at < 0 {
return Anchor{}, false
}
path, lines, ok := strings.Cut(link.URL[at+len(marker):], "#")
if !ok {
return Anchor{}, false
}
if i := strings.IndexByte(path, '?'); i >= 0 {
path = path[:i]
}
fields := fragment.FindStringSubmatch(lines)
if path == "" || fields == nil {
return Anchor{}, false
}
start, err := strconv.Atoi(fields[1])
if err != nil || start < 1 {
return Anchor{}, false
}
end := start
if fields[2] != "" {
end, err = strconv.Atoi(fields[2])
if err != nil || end < start {
return Anchor{}, false
}
}
return Anchor{
Line: link.Line, URL: link.URL, Repo: link.Owner + "/" + link.Name,
SHA: link.Ref, Path: path, Start: start, End: end,
}, true
}Four things have to hold. The kind must be KindBlob: a tree link, a commit link and a raw link all name something, but none of them names lines of a file in a form the tool can slice. Pinned must report a full commit, so a branch link anchors nothing even though it resolves today, and that is the premise of the whole project, because a reader following blob/main/... next month arrives at different code. The fragment must be L and a number, with an optional -L end that is not below the start. And the path must be non-empty, with any query string cut off before it is used, since a viewing preference appended to the URL is not part of the filename.
The first link that qualifies on the nearest paragraph becomes the Anchor, carrying the repository, the commit, the path and the line range. That is everything check needs to fetch the blob and slice it, and everything pin needs to write a lock line.
The third verdict is the point#
A block that comes out of this with no anchor is not an error. It gets ReasonUnanchored, whose severity is Listed: check prints it and counts it as neither a failure nor something it could not decide. pin skips it when it builds the lock:
for _, block := range quote.Blocks(article, source) {
if block.Anchor == nil {
continue
}
anchor := *block.Anchor
at := links.Link{File: article, Line: block.Fence, URL: anchor.URL}
if anchor.Lines() != len(block.Lines) {
problems = append(problems, Problem{Link: at, Err: fmt.Errorf(
"the permalink names %s, %d line(s) of %s, but the block holds %d; name the lines the block quotes and nothing else",
anchor.Range(), anchor.Lines(), anchor.Path, len(block.Lines))})
continue
}So a block has three possible fates. It is recorded and later compared byte for byte. It is refused with a reason, including the case above, where the permalink names a different number of lines than the block holds, which could never match the blob and so is reported instead of written. Or it is listed as unanchored, and no claim is made about it in either direction.
That third state is what makes the first one worth anything. srcproof exits 0 when every link names a commit, every quote is the code it pins and every code span names something at that commit; 1 when one of those fails; 2 when something could not be decided, because a commit the local clone does not have is undecided and never a pass. Fold unanchored blocks into the pass and "every quote verified" starts to mean "the quotes I happened to recognise were verified", the kind of green check that teaches people to stop reading output. Printing them keeps the report literal: this is what was checked, and this is what was not.
It also gives you the only diagnostic that matters. An unanchored block in the output is either an illustration, in which case the line is noise you learn to read past, or a quote you forgot to pin, in which case you have just been told.
What I would do differently#
I would keep the rule as it is. The gap I notice in use is that the output cannot distinguish "I never meant this block as a quote" from "I meant it and forgot the permalink", because both are one Listed line. The fix I would want is not a smarter guess but a stricter mode an author opts into for a directory where every block is supposed to be pinned, so the ambiguity is settled by a flag rather than by inference about intent. The moment a tool starts inferring intent from content, it is back to fuzzy matching with extra steps.
The other thing I would resist, and have, is a "nearest permalink within N lines" fallback. It would let a handful of articles pass without edits and would make the rule unexplainable, which is the worse trade. A verification tool is only as useful as your ability to predict its verdict without running it.
This article is a case in point. It quotes srcproof's own code and pins none of it, so srcproof would list every block above as unanchored and claim nothing about any of them, which is the correct reading of a post about the rule rather than a post pinned to the code.