Browse Source

fix: link media is modifying cache (#550)

Co-authored-by: Jon Lundy <jon@xuu.cc>
Reviewed-on: #550
Co-authored-by: xuu <xuu@noreply@mills.io>
Co-committed-by: xuu <xuu@noreply@mills.io>
pull/551/head
xuu 3 weeks ago
parent
commit
767185ddff
  1. 9
      internal/utils.go
  2. 18
      types/lextwt/ast.go
  3. 2
      types/lextwt/lextwt_test.go
  4. 8
      types/lextwt/parser.go

9
internal/utils.go

@ -1633,9 +1633,12 @@ func FormatTwtFactory(conf *Config, cache *Cache, archive Archiver) func(twt typ
renderer := html.NewRenderer(opts)
// copy alt to title if present.
for _, m := range twt.Links() {
if link, ok := m.(*lextwt.Link); ok {
link.TextToTitle()
if cp, ok := twt.(*lextwt.Twt); ok {
twt = cp.Clone()
for _, m := range twt.Links() {
if link, ok := m.(*lextwt.Link); ok {
link.TextToTitle()
}
}
}

18
types/lextwt/ast.go

@ -523,10 +523,20 @@ const (
)
func NewLink(text, target string, linkType LinkType) *Link {
return &Link{linkType, text, target, text}
return &Link{
linkType: linkType,
text: text,
target: target,
title: "",
}
}
func NewMedia(alt, target, title string) *Link {
return &Link{LinkMedia, alt, target, title}
return &Link{
linkType: LinkMedia,
text: alt,
target: target,
title: title,
}
}
func (n *Link) Clone() Elem {
@ -539,7 +549,7 @@ func (n *Link) Clone() Elem {
}
func (n *Link) TextToTitle() {
if n.title == "" {
n.title = n.text
n.title = `"` + n.text + `"`
}
}
func (n *Link) IsNil() bool { return n == nil }
@ -551,7 +561,7 @@ func (n *Link) Literal() string {
return fmt.Sprintf("<%s>", n.target)
case LinkMedia:
if n.title != "" {
return fmt.Sprintf(`![%s](%s "%s")`, n.text, n.target, n.title)
return fmt.Sprintf(`![%s](%s %s)`, n.text, n.target, n.title)
}
return fmt.Sprintf("![%s](%s)", n.text, n.target)
default:

2
types/lextwt/lextwt_test.go

@ -714,7 +714,7 @@ func TestParseTwt(t *testing.T) {
twt: lextwt.NewTwt(
twter,
lextwt.NewDateTime(parseTime("2021-11-05T22:00:00+01:00"), "2021-11-05T22:00:00+01:00"),
lextwt.NewMedia("alt", "https://example.com/image.png", "a title"),
lextwt.NewMedia("alt", "https://example.com/image.png", `"a title"`),
),
},
}

8
types/lextwt/parser.go

@ -818,10 +818,10 @@ func (p *parser) ParseLink() *Link {
if p.curTokenIs(TokSQUOTE, TokDQUOTE) {
end := p.curTok.Type
p.push()
p.append(p.curTok.Literal...) // ' or "
p.next()
p.push()
for !p.curTokenIs(end, TokRBRACK, TokLBRACK, TokRPAREN, TokLPAREN, TokEOF) {
p.append(p.curTok.Literal...) // text
p.next()
@ -832,11 +832,13 @@ func (p *parser) ParseLink() *Link {
p.next()
}
}
link.title = p.Literal()
p.pop()
p.append(p.curTok.Literal...) // ' or " (matching above)
p.next()
link.title = p.Literal()
p.pop()
}
p.pop()
}

Loading…
Cancel
Save