MCPcopy Create free account
hub / github.com/danielmiessler/Fabric / ExtractFencedCodeBlock

Function ExtractFencedCodeBlock

internal/domain/codeblock.go:13–45  ·  view source on GitHub ↗

ExtractFencedCodeBlock returns the content of the first (or, if last is true, the last) Markdown fenced code block in text, without the fence lines. A fence is a line of three or more backticks or tildes, optionally indented by up to three spaces and followed by an info string such as a language ta

(text string, last bool)

Source from the content-addressed store, hash-verified

11// as the opening fence. A block left open at the end of text runs to the end.
12// The second return value is false when text contains no fenced code block.
13func ExtractFencedCodeBlock(text string, last bool) (string, bool) {
14 var (
15 result string
16 found bool
17 inside bool
18 fence string
19 body []string
20 )
21
22 for line := range strings.SplitSeq(text, "\n") {
23 trimmed := strings.TrimRight(line, "\r")
24 if !inside {
25 if marker, ok := openingFence(trimmed); ok {
26 inside, fence, body = true, marker, nil
27 }
28 continue
29 }
30 if isClosingFence(trimmed, fence) {
31 inside = false
32 result, found = strings.Join(body, "\n"), true
33 if !last {
34 return result, true
35 }
36 continue
37 }
38 body = append(body, trimmed)
39 }
40
41 if inside {
42 result, found = strings.Join(body, "\n"), true
43 }
44 return result, found
45}
46
47// openingFence reports whether line opens a fenced code block. On success it
48// also returns the fence marker, the run of backticks or tildes.

Callers 2

handleChatProcessingFunction · 0.92

Calls 2

openingFenceFunction · 0.85
isClosingFenceFunction · 0.85

Tested by 1