MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / inlineCodeRanges

Function inlineCodeRanges

internal/links/extract.go:370–451  ·  view source on GitHub ↗

inlineCodeRanges returns half-open `[start, end)` byte ranges that cover every inline-code span in `content`, skipping over the fenced regions caller has already identified. CommonMark §6.1: an inline-code span is opened by a run of N consecutive backticks and CLOSED by the next run of EXACTLY N co

(content string, fenced [][2]int)

Source from the content-addressed store, hash-verified

368// is treated as literal text, not the start of a multi-line span.
369// Matches CommonMark behavior closely enough for our use.
370func inlineCodeRanges(content string, fenced [][2]int) [][2]int {
371 var ranges [][2]int
372 i := 0
373 n := len(content)
374 fi := 0 // cursor into fenced ranges
375 for i < n {
376 // Skip ahead past any fenced range that contains or
377 // precedes our cursor.
378 for fi < len(fenced) && fenced[fi][1] <= i {
379 fi++
380 }
381 if fi < len(fenced) && fenced[fi][0] <= i {
382 i = fenced[fi][1]
383 fi++
384 continue
385 }
386 // Look for the next backtick.
387 b := strings.IndexByte(content[i:], '`')
388 if b < 0 {
389 return ranges
390 }
391 openStart := i + b
392 // If the backtick is inside a fenced range, skip past it.
393 if fi < len(fenced) && fenced[fi][0] <= openStart && openStart < fenced[fi][1] {
394 i = fenced[fi][1]
395 fi++
396 continue
397 }
398 // Count the opener's backtick run.
399 j := openStart + 1
400 for j < n && content[j] == '`' {
401 j++
402 }
403 openLen := j - openStart
404 // Scan for a closing backtick RUN of EXACTLY `openLen`
405 // backticks. CommonMark §6.1 allows code spans to cross
406 // single newlines BUT a blank line (a line containing only
407 // whitespace) ends the enclosing paragraph and therefore
408 // terminates the span. We allow single-line wraps but break
409 // on blank lines.
410 closerStart := -1
411 closerEnd := -1
412 k := j
413 for k < n {
414 if content[k] == '\n' {
415 // Look at the next line: if it's blank
416 // (only whitespace before the next newline
417 // or EOF), the span ends here unmatched.
418 if isBlankLineAt(content, k+1, n) {
419 break
420 }
421 k++
422 continue
423 }
424 if content[k] != '`' {
425 k++
426 continue
427 }

Callers 2

ExtractWikiLinksFunction · 0.70
ExtractMentionsFunction · 0.70

Calls 2

appendFunction · 0.85
isBlankLineAtFunction · 0.70

Tested by

no test coverage detected