-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
401 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package textmeasure | ||
|
||
import ( | ||
"bytes" | ||
"net/url" | ||
"strings" | ||
|
||
"golang.org/x/net/html" | ||
) | ||
|
||
func sanitizeLinks(input string) (string, error) { | ||
doc, err := html.Parse(strings.NewReader(input)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var process func(*html.Node) | ||
process = func(n *html.Node) { | ||
if n.Type == html.ElementNode { | ||
for i, a := range n.Attr { | ||
if a.Key == "href" { | ||
if u, err := url.Parse(a.Val); err == nil { | ||
if u.RawQuery != "" { | ||
q := u.Query() | ||
u.RawQuery = q.Encode() | ||
} | ||
n.Attr[i].Val = u.String() | ||
} | ||
} | ||
} | ||
} | ||
for c := n.FirstChild; c != nil; c = c.NextSibling { | ||
process(c) | ||
} | ||
} | ||
process(doc) | ||
|
||
var buf bytes.Buffer | ||
if doc.FirstChild != nil && doc.FirstChild.FirstChild != nil { | ||
body := doc.FirstChild.LastChild | ||
if body != nil { | ||
for c := body.FirstChild; c != nil; c = c.NextSibling { | ||
// NOTE: this has the effect of also HTML rendering nodes, which is | ||
// robust to some malformed tags like <br>. Since this works in most | ||
// HTML contexts anyway, this is not unintended | ||
err = html.Render(&buf, c) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return buf.String(), nil | ||
} | ||
} | ||
return input, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
testdata/d2compiler/TestCompile/markdown_ampersand.exp.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
115 changes: 109 additions & 6 deletions
115
testdata/d2compiler/TestCompile/unsemantic_markdown.exp.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.