Skip to content

Commit

Permalink
support for hacky markdown comments (loose them, simply silenty ignore)
Browse files Browse the repository at this point in the history
  • Loading branch information
amberpixels committed Jan 3, 2025
1 parent 0267340 commit 73410e8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
6 changes: 5 additions & 1 deletion internal/jalapeno/builder_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func (b *NtBlockBuilder) DecorateWith(d func(source []byte, block nt.Block)) {
func (builders NtBlockBuilders) Build(source []byte) []nt.Block {
result := make([]nt.Block, 0)
for _, builder := range builders {
result = append(result, builder.Build(source))
// Some nodes (e.g. markdown hacky comments) can be handled as nil empty blocks
// let's just filter them out here
if built := builder.Build(source); built != nil {
result = append(result, built)
}
}

return result
Expand Down
11 changes: 11 additions & 0 deletions internal/jalapeno/helpers_notionapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package jalapeno

import (
"strings"

nt "github.com/jomei/notionapi"
)

func sanitizeBlockLanguage(language string) string {
Expand All @@ -11,6 +13,15 @@ func sanitizeBlockLanguage(language string) string {
return language
}

func nonEmptyRichTexts(rts []nt.RichText) []nt.RichText {
for i, rt := range rts {
if rt.PlainText == "" {
rts = append(rts[:i], rts[i+1:]...)
}
}
return rts
}

// html2notion is a hacky function that converts HTML to Notion-compatible text
// It's very simple, and in future is considered to be more complex
// Deprecated: don't tend to use it very often, it's subject to change
Expand Down
33 changes: 24 additions & 9 deletions internal/jalapeno/jalapeno.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ func ToRichText(node mdast.Node) *NtRichTextBuilder {
)
return nt.NewTextRichText(content)
})

case *mdast.TextBlock:
return NewNtRichTextBuilder(func(source []byte) *nt.RichText {
return nt.NewTextRichText(string(contentFromLines(v, source)))
})
default:
return nil
}
Expand Down Expand Up @@ -220,6 +223,8 @@ func ToBlocks(node mdast.Node) (result NtBlockBuilders) {
return handleTable(node)
case mdast.KindHTMLBlock:
return handleHTMLBlock(node)
case mdast.KindTextBlock:
return handleTextBlock(node)
}

if node.ChildCount() == 0 {
Expand Down Expand Up @@ -256,14 +261,7 @@ func ToBlocks(node mdast.Node) (result NtBlockBuilders) {
case mdast.KindLink:
return handleBlockLink(node)
case mdast.KindTextBlock:
richTexts := ExtractRichTexts(node)
return NtBlockBuilders{
NewNtBlockBuilder(func(source []byte) nt.Block {
return nt.NewQuoteBlock(nt.Quote{
RichText: richTexts.Build(source),
})
}),
}
return handleTextBlock(node)
}

panic(fmt.Sprintf("unhandled node type: %s", node.Kind().String()))
Expand Down Expand Up @@ -473,6 +471,23 @@ func handleBlockLink(node mdast.Node) NtBlockBuilders {
return handleImage(image, linkDecorator(string(link.Destination)))
}

func handleTextBlock(node mdast.Node) NtBlockBuilders {
richTexts := ExtractRichTexts(node)

return NtBlockBuilders{
NewNtBlockBuilder(func(source []byte) nt.Block {
rts := nonEmptyRichTexts(richTexts.Build(source))
if len(rts) == 0 {
return nil
}

return nt.NewParagraphBlock(nt.Paragraph{
RichText: rts,
})
}),
}
}

// handleListItem handles MD's list item and its children
// List Item on markdown can have children. For notion - first child is usually a RichText
// Other children are built as nested blocks
Expand Down
11 changes: 11 additions & 0 deletions internal/jalapeno/jalapeno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,17 @@ func main() {
nt.NewDividerBlock(),
})

f("hack: markdown comment", `[a hidden comment]: #`, nt.Blocks{})

f("hack: comment between lines", `
---
[a hidden comment]: #
---`,
nt.Blocks{
nt.NewDividerBlock(),
nt.NewDividerBlock(),
})

// --------------
// --- HTML -----
// --------------
Expand Down

0 comments on commit 73410e8

Please sign in to comment.