Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions internal/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
Callout // > [!NOTE] callout/admonition
Table // GFM pipe table
Kanban // ```kanban``` fenced kanban board
Bookmark // [title](url) link card
Link // [title](url) link card
)

// String returns the human-readable name of a BlockType.
Expand Down Expand Up @@ -63,8 +63,8 @@ func (bt BlockType) String() string {
return "Table"
case Kanban:
return "Kanban"
case Bookmark:
return "Bookmark"
case Link:
return "Link"
default:
return "Unknown"
}
Expand Down Expand Up @@ -113,8 +113,8 @@ func (bt BlockType) Short() string {
return "tb"
case Kanban:
return "kb"
case Bookmark:
return "bm"
case Link:
return "ln"
default:
return "?"
}
Expand Down Expand Up @@ -278,10 +278,10 @@ func ExtractDefinition(content string) (term, definition string) {
return first, ""
}

// ExtractBookmark splits a bookmark block's content into its title line
// ExtractLink splits a link block's content into its title line
// (first line) and URL (second line). When only a URL is stored, title
// is empty.
func ExtractBookmark(content string) (title, url string) {
func ExtractLink(content string) (title, url string) {
first, rest, found := strings.Cut(content, "\n")
if found {
return first, rest
Expand Down
20 changes: 10 additions & 10 deletions internal/block/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
)

var (
bookmarkLinkRe = regexp.MustCompile(`^\[([^\]]*)\]\((https?://[^\s)]+)\)\s*$`)
bookmarkBareRe = regexp.MustCompile(`^(https?://\S+)\s*$`)
linkLinkRe = regexp.MustCompile(`^\[([^\]]*)\]\((https?://[^\s)]+)\)\s*$`)
linkBareRe = regexp.MustCompile(`^(https?://\S+)\s*$`)
)

// ParseBookmark reports whether a single line is a bookmark — either a
// ParseLink reports whether a single line is a link — either a
// titled markdown link `[title](url)` or a bare http(s) URL — and returns
// the extracted title (may be empty) and URL.
func ParseBookmark(line string) (title, url string, ok bool) {
if m := bookmarkLinkRe.FindStringSubmatch(line); m != nil {
func ParseLink(line string) (title, url string, ok bool) {
if m := linkLinkRe.FindStringSubmatch(line); m != nil {
return m[1], m[2], true
}
if m := bookmarkBareRe.FindStringSubmatch(line); m != nil {
if m := linkBareRe.FindStringSubmatch(line); m != nil {
return "", m[1], true
}
return "", "", false
Expand Down Expand Up @@ -221,13 +221,13 @@ func Parse(markdown string) []Block {
continue
}

// --- Bookmark ([title](url) or bare URL on its own line) ---
if title, url, ok := ParseBookmark(line); ok {
// --- Link ([title](url) or bare URL on its own line) ---
if title, url, ok := ParseLink(line); ok {
content := url
if title != "" {
content = title + "\n" + url
}
blocks = append(blocks, Block{Type: Bookmark, Content: content})
blocks = append(blocks, Block{Type: Link, Content: content})
i++
continue
}
Expand Down Expand Up @@ -300,7 +300,7 @@ func isBlockStart(line string) bool {
if strings.HasPrefix(line, "![[") && strings.HasSuffix(line, "]]") {
return true
}
if _, _, ok := ParseBookmark(line); ok {
if _, _, ok := ParseLink(line); ok {
return true
}
return isDivider(line)
Expand Down
26 changes: 13 additions & 13 deletions internal/block/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,46 +313,46 @@ func TestParse(t *testing.T) {
},
},
{
name: "bookmark titled link",
name: "link titled link",
input: "[Example](https://example.com)",
expect: []Block{
{Type: Bookmark, Content: "Example\nhttps://example.com"},
{Type: Link, Content: "Example\nhttps://example.com"},
},
},
{
name: "bookmark bare url",
name: "link bare url",
input: "https://example.com",
expect: []Block{
{Type: Bookmark, Content: "https://example.com"},
{Type: Link, Content: "https://example.com"},
},
},
{
name: "bookmark http url",
name: "link http url",
input: "http://example.com/path?q=1",
expect: []Block{
{Type: Bookmark, Content: "http://example.com/path?q=1"},
{Type: Link, Content: "http://example.com/path?q=1"},
},
},
{
name: "bookmark between paragraphs",
name: "link between paragraphs",
input: "above\n\n[Site](https://site.io)\n\nbelow",
expect: []Block{
{Type: Paragraph, Content: "above"},
{Type: Paragraph, Content: ""},
{Type: Bookmark, Content: "Site\nhttps://site.io"},
{Type: Link, Content: "Site\nhttps://site.io"},
{Type: Paragraph, Content: ""},
{Type: Paragraph, Content: "below"},
},
},
{
name: "bookmark titled link with empty title",
name: "link titled link with empty title",
input: "[](https://example.com)",
expect: []Block{
{Type: Bookmark, Content: "https://example.com"},
{Type: Link, Content: "https://example.com"},
},
},
{
name: "inline link in paragraph not parsed as bookmark",
name: "inline link in paragraph not parsed as link",
input: "see [docs](https://example.com) for details",
expect: []Block{
{Type: Paragraph, Content: "see [docs](https://example.com) for details"},
Expand Down Expand Up @@ -417,8 +417,8 @@ func formatBlocks(blocks []Block) string {
b.WriteString("Embed")
case Table:
b.WriteString("Table")
case Bookmark:
b.WriteString("Bookmark")
case Link:
b.WriteString("Link")
}
if bl.Content != "" {
b.WriteString(" " + bl.Content)
Expand Down
4 changes: 2 additions & 2 deletions internal/block/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func Serialize(blocks []Block) string {
case Embed:
lines = append(lines, "![["+b.Content+"]]")

case Bookmark:
title, url := ExtractBookmark(b.Content)
case Link:
title, url := ExtractLink(b.Content)
if title == "" {
lines = append(lines, url)
} else {
Expand Down
6 changes: 3 additions & 3 deletions internal/block/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ func TestSerializeRoundTrip(t *testing.T) {
md: "| Name | Age |\n| ---- | --- |",
},
{
name: "bookmark titled link",
name: "link titled link",
md: "[Example](https://example.com)",
},
{
name: "bookmark bare url",
name: "link bare url",
md: "https://example.com",
},
{
name: "bookmark between paragraphs",
name: "link between paragraphs",
md: "above\n\n[Site](https://site.io)\n\nbelow",
},
}
Expand Down
Loading
Loading