fix(markup): only escape innermost markup

Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2023-05-29 13:34:42 +05:30
parent ce73ad4dfc
commit 8e68c7bcc2
4 changed files with 580 additions and 789 deletions

View File

@ -13,7 +13,7 @@
<title>RIP Medium</title> <title>RIP Medium</title>
<script> <script>
// On page load or when changing themes, best to add inline in `head` to avoid FOUC // On page load or when changing themes, best to add inline in `head` to avoid FOUC
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark') document.documentElement.classList.add('dark')
} else { } else {

View File

@ -82,36 +82,39 @@ func ConvertMarkup(text string, markups []entities.Markup) string {
utf16Text := utf16.Encode([]rune(text)) utf16Text := utf16.Encode([]rune(text))
ranged := utf16Text[r.Range[0]:r.Range[1]] ranged := utf16Text[r.Range[0]:r.Range[1]]
textToWrap := string(utf16.Decode(ranged)) textToWrap := string(utf16.Decode(ranged))
markedUp.WriteString(wrapInMarkups(textToWrap, r.Markups)) markedUp.WriteString(wrapInMarkups(textToWrap, r.Markups, false))
} }
return markedUp.String() 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 { if len(markups) == 0 {
return child return child
} }
if !childIsMarkup {
child = html.EscapeString(child)
}
markedUp := markupNodeInContainer(child, markups[0]) markedUp := markupNodeInContainer(child, markups[0])
return wrapInMarkups(markedUp, markups[1:]) return wrapInMarkups(markedUp, markups[1:], true)
} }
func markupNodeInContainer(child string, markup entities.Markup) string { func markupNodeInContainer(child string, markup entities.Markup) string {
switch markup.Type { switch markup.Type {
case "A": case "A":
if markup.Href != nil { 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 { } 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": case "CODE":
return fmt.Sprintf(`<code>%s</code>`, html.EscapeString(child)) return fmt.Sprintf(`<code>%s</code>`, child)
case "EM": case "EM":
return fmt.Sprintf(`<em>%s</em>`, html.EscapeString(child)) return fmt.Sprintf(`<em>%s</em>`, child)
case "STRONG": case "STRONG":
return fmt.Sprintf(`<strong>%s</strong>`, html.EscapeString(child)) return fmt.Sprintf(`<strong>%s</strong>`, child)
default: default:
return fmt.Sprintf(`<code>%s</code>`, html.EscapeString(child)) return fmt.Sprintf(`<code>%s</code>`, child)
} }
return html.EscapeString(child) return child
} }

View File

@ -58,7 +58,10 @@ func ConvertParagraphs(paragraphs []entities.Paragraph) string {
} }
switch p.Type { 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) children := ConvertMarkup(p.Text, p.Markups)
ps.WriteString(fmt.Sprintf("<blockquote><p>%s</p></blockquote>", children)) ps.WriteString(fmt.Sprintf("<blockquote><p>%s</p></blockquote>", children))
case "H2": case "H2":
@ -132,7 +135,7 @@ func convertOli(ps []entities.Paragraph) (string, int) {
break break
} }
} }
return sb.String(), count return sb.String(), count
} }
@ -149,6 +152,6 @@ func convertUli(ps []entities.Paragraph) (string, int) {
break break
} }
} }
return sb.String(), count return sb.String(), count
} }

File diff suppressed because it is too large Load Diff