Update MCP to handle markdown for all document content tools + fix converter edge case for code fence closing
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -55,6 +55,20 @@ func ParseMarkdown(markdown string) (Node, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// normalizeCodeBlockContent strips one trailing newline when it is only the
|
||||
// line terminator goldmark attaches to the last content line. That avoids an
|
||||
// extra visible blank line in editors (e.g. TipTap) while preserving a
|
||||
// trailing blank line in the source, which ends with two newlines.
|
||||
func normalizeCodeBlockContent(content string) string {
|
||||
if content == "" {
|
||||
return content
|
||||
}
|
||||
if strings.HasSuffix(content, "\n") && !strings.HasSuffix(content, "\n\n") {
|
||||
return strings.TrimSuffix(content, "\n")
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
type converter struct {
|
||||
source []byte
|
||||
marks []Mark
|
||||
@@ -238,7 +252,7 @@ func (c *converter) convertFencedCodeBlock(n *ast.FencedCodeBlock) ([]Node, erro
|
||||
buf.Write(line.Value(c.source))
|
||||
}
|
||||
|
||||
content := buf.String()
|
||||
content := normalizeCodeBlockContent(buf.String())
|
||||
|
||||
var lang *string
|
||||
if n.Language(c.source) != nil {
|
||||
@@ -274,7 +288,7 @@ func (c *converter) convertCodeBlock(n *ast.CodeBlock) ([]Node, error) {
|
||||
buf.Write(line.Value(c.source))
|
||||
}
|
||||
|
||||
content := buf.String()
|
||||
content := normalizeCodeBlockContent(buf.String())
|
||||
|
||||
attrs, err := json.Marshal(CodeBlockAttrs{Language: nil})
|
||||
if err != nil {
|
||||
|
||||
@@ -167,7 +167,7 @@ func TestParseMarkdown_CodeBlock(t *testing.T) {
|
||||
assert.Equal(t, "go", *attrs.Language)
|
||||
|
||||
require.Len(t, cb.Content, 1)
|
||||
assert.Equal(t, "fmt.Println(\"hello\")\n", *cb.Content[0].Text)
|
||||
assert.Equal(t, "fmt.Println(\"hello\")", *cb.Content[0].Text)
|
||||
})
|
||||
|
||||
t.Run("without language", func(t *testing.T) {
|
||||
@@ -183,6 +183,21 @@ func TestParseMarkdown_CodeBlock(t *testing.T) {
|
||||
attrs, err := cb.CodeBlockAttrs()
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, attrs.Language)
|
||||
|
||||
require.Len(t, cb.Content, 1)
|
||||
assert.Equal(t, "some code", *cb.Content[0].Text)
|
||||
})
|
||||
|
||||
t.Run("trailing blank line preserved", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
doc, err := ParseMarkdown("```\nline\n\n```")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, doc.Content, 1)
|
||||
|
||||
cb := doc.Content[0]
|
||||
require.Len(t, cb.Content, 1)
|
||||
assert.Equal(t, "line\n\n", *cb.Content[0].Text)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user