Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable auto email mailto: links #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions ansi/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
u := string(n.URL(source))
label := string(n.Label(source))
if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(u), "mailto:") {
if tr.context.options.DisableAutoEmailLinks {
return Element{
Copy link
Member

@bashbunni bashbunni Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the contribution! This is a feature I know people are interested in.

One thing to keep in mind is this also strips the link styling of anything that looks like an email. Do we want to keep the ability to style these components with custom styles? Would be great to hear your thoughts :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 Thank you for the review!

For my own use case, I would prefer to just remove the styling for anything that looks like an email. But, I can totally see other people's use case where they want to modify the style instead.

Happy to modify this PR if you think we should not just disable email auto links, but allow them to be disabled or otherwise manipulated through a custom style. But, I might need a little more time to get that sorted out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 @bashbunni, just wanted to follow up on this again. Should I try to update this PR to keep the ability to style these components?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @picatz I'm going to leave it open for now, I'm currently doing a big refactor of glamour, so will take a look again once that's done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, thank you for the update!

Renderer: &BaseElement{
Token: label,
},
}
}
u = "mailto:" + u
}

Expand Down
11 changes: 6 additions & 5 deletions ansi/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (

// Options is used to configure an ANSIRenderer.
type Options struct {
BaseURL string
WordWrap int
PreserveNewLines bool
ColorProfile termenv.Profile
Styles StyleConfig
BaseURL string
WordWrap int
PreserveNewLines bool
DisableAutoEmailLinks bool
ColorProfile termenv.Profile
Styles StyleConfig
}

// ANSIRenderer renders markdown content as ANSI escaped sequences.
Expand Down
8 changes: 8 additions & 0 deletions glamour.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ func WithEmoji() TermRendererOption {
}
}

// WithDisabledMailtoLinks sets TermRenderer's option to disable adding mailto links.
func WithDisableAutoEmailLinks(v bool) TermRendererOption {
return func(tr *TermRenderer) error {
tr.ansiOptions.DisableAutoEmailLinks = v
return nil
}
}

func (tr *TermRenderer) Read(b []byte) (int, error) {
return tr.renderBuf.Read(b)
}
Expand Down
21 changes: 21 additions & 0 deletions glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ func TestWithEmoji(t *testing.T) {
}
}

func TestDisableAutoEmailLinks(t *testing.T) {
r, err := NewTermRenderer(
WithDisableAutoEmailLinks(true),
)
if err != nil {
t.Fatal(err)
}

goModuleExample := "This is my favorite go module: golang.org/x/[email protected]"

b, err := r.Render(goModuleExample)
if err != nil {
t.Fatal(err)
}
b = strings.TrimSpace(b)

if b != goModuleExample {
t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", goModuleExample, b)
}
}

func TestWithPreservedNewLines(t *testing.T) {
r, err := NewTermRenderer(
WithPreservedNewLines(),
Expand Down