injectChipSentinels rewrites the source so each top-level fenced code block with a declared language is preceded by a sentinel paragraph. Returns the rewritten source and whether any sentinel was added. Reconstructing the document from the segments is loss-less: splitTopLevelCodeBlocks partitions th
(input string)
| 62 | // the lines contiguously, so joining the parts with "\n" reproduces the |
| 63 | // original text (plus the inserted sentinels). |
| 64 | func injectChipSentinels(input string) (string, bool) { |
| 65 | segs := splitTopLevelCodeBlocks(input) |
| 66 | has := false |
| 67 | parts := make([]string, 0, len(segs)) |
| 68 | for _, s := range segs { |
| 69 | if s.isCode && s.lang != "" { |
| 70 | // Blank line + sentinel line, so glamour treats the sentinel as a |
| 71 | // standalone paragraph immediately above the code block. |
| 72 | parts = append(parts, "\n"+chipSentinelOpen+s.lang+chipSentinelClose+"\n"+s.text) |
| 73 | has = true |
| 74 | } else { |
| 75 | parts = append(parts, s.text) |
| 76 | } |
| 77 | } |
| 78 | return strings.Join(parts, "\n"), has |
| 79 | } |
| 80 | |
| 81 | // replaceChipSentinels swaps each rendered sentinel line for the themed |
| 82 | // language chip. It matches on the raw line (the marker survives as one |
no test coverage detected