MCPcopy
hub / github.com/expr-lang/expr / extractCodeBlocksWithTitle

Function extractCodeBlocksWithTitle

test/examples/markdown.go:13–59  ·  view source on GitHub ↗
(markdown string)

Source from the content-addressed store, hash-verified

11}
12
13func extractCodeBlocksWithTitle(markdown string) []CodeBlock {
14 var blocks []CodeBlock
15 var currentBlock []string
16 var currentTitle string
17 inBlock := false
18
19 // Split the markdown into lines.
20 lines := strings.Split(markdown, "\n")
21 for i, line := range lines {
22 trimmed := strings.TrimSpace(line)
23 // Check if the line starts with a code block fence.
24 if strings.HasPrefix(trimmed, "```") {
25 // If already inside a code block, this marks its end.
26 if inBlock {
27 blocks = append(blocks, CodeBlock{
28 Title: currentTitle,
29 Content: strings.Join(currentBlock, "\n"),
30 })
31 currentBlock = nil
32 inBlock = false
33 currentTitle = ""
34 } else {
35 // Not in a block: starting a new code block.
36 // Look backwards for the closest non-empty line that is not a code fence.
37 title := ""
38 for j := i - 1; j >= 0; j-- {
39 prev := strings.TrimSpace(lines[j])
40 if prev == "" || strings.HasPrefix(prev, "```") {
41 continue
42 }
43 title = prev
44 break
45 }
46 currentTitle = title
47 inBlock = true
48 }
49 // Skip the fence line.
50 continue
51 }
52
53 // If inside a code block, add the line.
54 if inBlock {
55 currentBlock = append(currentBlock, line)
56 }
57 }
58 return blocks
59}

Callers 1

initFunction · 0.85

Calls 1

SplitMethod · 0.80

Tested by 1

initFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…