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

feat(parser): support bare URLs #912

Merged
merged 1 commit into from
Jan 30, 2022
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
2 changes: 2 additions & 0 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ func serialize(content []interface{}) ([]byte, *placeholders) {
result.WriteString(p.String())
case *types.StringElement:
result.WriteString(element.Content)
case *types.SpecialCharacter:
result.WriteString(element.Name) // reserialize, so we can detect bare URLs (eg: `a link to <{base_url}>`)
default:
// replace with placeholder
p := placeholders.add(element)
Expand Down
12 changes: 6 additions & 6 deletions pkg/parser/document_processing_parse_fragments.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func (c *current) disableFrontMatterRule() {

func (c *current) isDocumentHeaderAllowed() bool {
allowed, found := c.globalStore[documentHeaderKey].(bool)
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("checking if DocumentHeader is allowed: %t", found && allowed && !c.isWithinDelimitedBlock())
}
// if log.IsLevelEnabled(log.DebugLevel) {
// log.Debugf("checking if DocumentHeader is allowed: %t", found && allowed && !c.isWithinDelimitedBlock())
// }
return found && allowed && !c.isWithinDelimitedBlock()
}

Expand All @@ -138,9 +138,9 @@ func (c *current) disableDocumentHeaderRule(element interface{}) {
// do not disable yet
return
default:
if log.IsLevelEnabled(log.DebugLevel) {
log.Debugf("disabling DocumentHeader after parsing element of type '%T'", element)
}
// if log.IsLevelEnabled(log.DebugLevel) {
// log.Debugf("disabling DocumentHeader after parsing element of type '%T'", element)
// }
c.globalStore[documentHeaderKey] = false

}
Expand Down
169 changes: 169 additions & 0 deletions pkg/parser/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,175 @@ var _ = Describe("links", func() {

Context("in final documents", func() {

Context("bare URLs", func() {

It("should parse standalone URL with scheme ", func() {
source := `<https://example.com>`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.InlineLink{
Location: &types.Location{
Scheme: "https://",
Path: "example.com",
},
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("should parse URL with scheme in sentence", func() {
source := `a link to <https://example.com>.`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "a link to ",
},
&types.InlineLink{
Location: &types.Location{
Scheme: "https://",
Path: "example.com",
},
},
&types.StringElement{
Content: ".",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("should parse substituted URL with scheme", func() {
source := `:example: https://example.com

a link to <{example}>.`
expected := &types.Document{
Elements: []interface{}{
&types.AttributeDeclaration{
Name: "example",
Value: "https://example.com",
},
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "a link to ",
},
&types.InlineLink{
Location: &types.Location{
Scheme: "https://",
Path: "example.com",
},
},
&types.StringElement{
Content: ".",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

Context("malformed", func() {

It("should not parse URL without scheme", func() {
source := `a link to <example.com>.`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "a link to ",
},
&types.SpecialCharacter{
Name: "<",
},
&types.StringElement{
Content: "example.com",
},
&types.SpecialCharacter{
Name: ">",
},
&types.StringElement{
Content: ".",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("should parse with special character in URL", func() {
source := `a link to https://example.com>[].`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "a link to ",
},
&types.InlineLink{
Location: &types.Location{
Scheme: "https://",
Path: []interface{}{
&types.StringElement{
Content: "example.com",
},
&types.SpecialCharacter{
Name: ">",
},
},
},
},
&types.StringElement{
Content: ".",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})

It("should parse with opening angle bracket", func() {
source := `a link to <https://example.com[].`
expected := &types.Document{
Elements: []interface{}{
&types.Paragraph{
Elements: []interface{}{
&types.StringElement{
Content: "a link to ",
},
&types.SpecialCharacter{
Name: "<",
},
&types.InlineLink{
Location: &types.Location{
Scheme: "https://",
Path: "example.com",
},
},
&types.StringElement{
Content: ".",
},
},
},
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
})
})
})

Context("external links", func() {

It("without text", func() {
Expand Down
Loading