Skip to content

Commit c9af045

Browse files
committed
feat(tables): format github links inside tables in a more readable manner
1 parent f2eb484 commit c9af045

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

ansi/elements.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"strings"
99

10+
"github.com/charmbracelet/glamour/internal/autolink"
1011
east "github.com/yuin/goldmark-emoji/ast"
1112
"github.com/yuin/goldmark/ast"
1213
astext "github.com/yuin/goldmark/extension/ast"
@@ -279,6 +280,9 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
279280
href: u,
280281
linkType: linkTypeAuto,
281282
}
283+
if shortned, ok := autolink.Detect(u); ok {
284+
tl.content = shortned
285+
}
282286
text := linkWithSuffix(tl, ctx.table.tableLinks)
283287

284288
renderer = &LinkElement{

ansi/table_links.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010
"strings"
1111

12+
"github.com/charmbracelet/glamour/internal/autolink"
1213
xansi "github.com/charmbracelet/x/ansi"
1314
"github.com/charmbracelet/x/exp/slice"
1415
"github.com/yuin/goldmark/ast"
@@ -135,6 +136,9 @@ func (e *TableElement) collectLinksAndImages(ctx RenderContext) error {
135136
content: linkDomain(uri),
136137
linkType: linkTypeAuto,
137138
}
139+
if shortned, ok := autolink.Detect(uri); ok {
140+
autoLink.content = shortned
141+
}
138142
links = append(links, autoLink)
139143
case *ast.Image:
140144
content, err := nodeContent(node, e.source)

ansi/testdata/TestRenderer/table_with_footer_auto_links_short.golden

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
| Description | Link |
2+
| -------------------- | ------------------------------------------------------------------------------- |
3+
| Issue | https://github.com/owner/repo/issue/123 |
4+
| Issue | https://github.com/owner/repo/issues/123 |
5+
| Issue Comment | https://github.com/owner/repo/issue/123#issuecomment-456 |
6+
| Issue Comment | https://github.com/owner/repo/issues/123#issuecomment-456 |
7+
| Pull Request | https://github.com/owner/repo/pull/123 |
8+
| Pull Request | https://github.com/owner/repo/pulls/123 |
9+
| Pull Request Comment | https://github.com/owner/repo/pull/123#issuecomment-456 |
10+
| Pull Request Comment | https://github.com/owner/repo/pulls/123#issuecomment-456 |
11+
| Pull Request Comment | https://github.com/owner/repo/pull/123#discussion_r456 |
12+
| Pull Request Comment | https://github.com/owner/repo/pulls/123#discussion_r456 |
13+
| Pull Request Review | https://github.com/owner/repo/pull/123#pullrequestreview-456 |
14+
| Pull Request Review | https://github.com/owner/repo/pulls/123#pullrequestreview-456 |
15+
| Discussion | https://github.com/owner/repo/discussions/123 |
16+
| Discussion Comment | https://github.com/owner/repo/discussions/123#discussioncomment-456 |
17+
| Commit | https://github.com/owner/repo/commit/abcdefghijklmnopqrsxyz |
18+
| Commit | https://github.com/owner/repo/commit/abcdefghijklmnopqrsxyz#diff-123 |
19+
| Pull Request Commit | https://github.com/owner/repo/pull/123/commits/abcdefghijklmnopqrsxyz |
20+
| Pull Request Commit | https://github.com/owner/repo/pulls/123/commits/abcdefghijklmnopqrsxyz |
21+
| Pull Request Commit | https://github.com/owner/repo/pull/123/commits/abcdefghijklmnopqrsxyz#diff-123 |
22+
| Pull Request Commit | https://github.com/owner/repo/pulls/123/commits/abcdefghijklmnopqrsxyz#diff-123 |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"link": {
3+
"color": "30",
4+
"underline": true
5+
},
6+
"link_text": {
7+
"color": "35",
8+
"bold": true
9+
}
10+
}

0 commit comments

Comments
 (0)