mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2024-12-26 01:25:57 +00:00
fix(markup): only escape innermost markup
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
parent
ce73ad4dfc
commit
8e68c7bcc2
@ -82,36 +82,39 @@ func ConvertMarkup(text string, markups []entities.Markup) string {
|
||||
utf16Text := utf16.Encode([]rune(text))
|
||||
ranged := utf16Text[r.Range[0]:r.Range[1]]
|
||||
textToWrap := string(utf16.Decode(ranged))
|
||||
markedUp.WriteString(wrapInMarkups(textToWrap, r.Markups))
|
||||
markedUp.WriteString(wrapInMarkups(textToWrap, r.Markups, false))
|
||||
}
|
||||
|
||||
return markedUp.String()
|
||||
}
|
||||
|
||||
func wrapInMarkups(child string, markups []entities.Markup) string {
|
||||
func wrapInMarkups(child string, markups []entities.Markup, childIsMarkup bool) string {
|
||||
if len(markups) == 0 {
|
||||
return child
|
||||
}
|
||||
if !childIsMarkup {
|
||||
child = html.EscapeString(child)
|
||||
}
|
||||
markedUp := markupNodeInContainer(child, markups[0])
|
||||
return wrapInMarkups(markedUp, markups[1:])
|
||||
return wrapInMarkups(markedUp, markups[1:], true)
|
||||
}
|
||||
|
||||
func markupNodeInContainer(child string, markup entities.Markup) string {
|
||||
switch markup.Type {
|
||||
case "A":
|
||||
if markup.Href != nil {
|
||||
return fmt.Sprintf(`<a href="%s">%s</a>`, *markup.Href, html.EscapeString(child))
|
||||
return fmt.Sprintf(`<a href="%s">%s</a>`, *markup.Href, child)
|
||||
} else if markup.UserID != nil {
|
||||
return fmt.Sprintf(`<a href="https://medium.com/u/%s">%s</a>`, markup.UserID, html.EscapeString(child))
|
||||
return fmt.Sprintf(`<a href="https://medium.com/u/%s">%s</a>`, markup.UserID, child)
|
||||
}
|
||||
case "CODE":
|
||||
return fmt.Sprintf(`<code>%s</code>`, html.EscapeString(child))
|
||||
return fmt.Sprintf(`<code>%s</code>`, child)
|
||||
case "EM":
|
||||
return fmt.Sprintf(`<em>%s</em>`, html.EscapeString(child))
|
||||
return fmt.Sprintf(`<em>%s</em>`, child)
|
||||
case "STRONG":
|
||||
return fmt.Sprintf(`<strong>%s</strong>`, html.EscapeString(child))
|
||||
return fmt.Sprintf(`<strong>%s</strong>`, child)
|
||||
default:
|
||||
return fmt.Sprintf(`<code>%s</code>`, html.EscapeString(child))
|
||||
return fmt.Sprintf(`<code>%s</code>`, child)
|
||||
}
|
||||
return html.EscapeString(child)
|
||||
return child
|
||||
}
|
||||
|
@ -58,7 +58,10 @@ func ConvertParagraphs(paragraphs []entities.Paragraph) string {
|
||||
}
|
||||
|
||||
switch p.Type {
|
||||
case "BQ", "MIXTAPE_EMBED", "PQ":
|
||||
case "BQ", "PQ":
|
||||
children := ConvertMarkup(p.Text, p.Markups)
|
||||
ps.WriteString(fmt.Sprintf("<blockquote><p>%s</p></blockquote>", children))
|
||||
case "MIXTAPE_EMBED":
|
||||
children := ConvertMarkup(p.Text, p.Markups)
|
||||
ps.WriteString(fmt.Sprintf("<blockquote><p>%s</p></blockquote>", children))
|
||||
case "H2":
|
||||
|
1333
response.json
1333
response.json
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user