Skip to content

Commit

Permalink
feat(parser/renderer): support email autolinks (#1056)
Browse files Browse the repository at this point in the history
Fixes #1055

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jun 24, 2022
1 parent 53ab4a5 commit a2de2b7
Show file tree
Hide file tree
Showing 13 changed files with 8,539 additions and 8,278 deletions.
2 changes: 1 addition & 1 deletion pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (c *conditions) eval() bool {
}

func contentOf(ctx *ParseContext, incl *types.FileInclusion) ([]byte, bool, error) {
path := incl.Location.Stringify()
path := incl.Location.ToString()
currentDir := filepath.Dir(ctx.filename)
filename := filepath.Join(currentDir, path)

Expand Down
24 changes: 22 additions & 2 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,19 @@ func reparseAttributesInElements(elements []interface{}, subs []string, opts ...
func reparseAttributes(e types.WithAttributes, subs []string, opts ...Option) error {
attributes := e.GetAttributes()
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("reparsing attributes in %s", spew.Sdump(attributes))
log.Debugf("reparsing attributes with subs='%s' in %s", strings.Join(subs, ","), spew.Sdump(attributes))
}
for k, v := range attributes {
switch k {
case types.AttrTitle, types.AttrXRefLabel, types.AttrInlineLinkText:
case types.AttrTitle, types.AttrXRefLabel:
v, err := ReparseAttributeValue(v, subs, opts...)
if err != nil {
return err
}
attributes[k] = types.Reduce(v)
case types.AttrInlineLinkText:
// same as above, but do not allow for inline macros (eg: links)
subs = removeSubstitution(subs, Macros)
v, err := ReparseAttributeValue(v, subs, opts...)
if err != nil {
return err
Expand Down Expand Up @@ -870,6 +878,18 @@ func substitutions(s string) ([]string, error) {
}
}

// removes the given `sub` from the given `subs`
func removeSubstitution(subs []string, sub string) []string {
result := make([]string, 0, len(subs))
for _, s := range subs {
if s == sub {
continue
}
result = append(result, s)
}
return result
}

const (
// enabledSubstitutions the key in which enabled substitutions are stored in the parser's GlobalStore
enabledSubstitutions string = "enabled_substitutions"
Expand Down
46 changes: 46 additions & 0 deletions pkg/parser/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,51 @@ a link to <{example}>.`
})
})

Context("email autolinks", func() {

It("valid email in paragraph", func() {
source := `write to [email protected].`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "write to ",
},
&types.InlineLink{
Location: &types.Location{
Scheme: "mailto:",
Path: "[email protected]",
},
},
&types.StringElement{
Content: ".",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("invalid email in paragraph", func() {
source := `write to [email protected].`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "write to [email protected].",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

})

Context("external links", func() {

It("without text", func() {
Expand Down Expand Up @@ -1740,5 +1785,6 @@ title]`
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})

})
})
Loading

0 comments on commit a2de2b7

Please sign in to comment.