Set image URLs to absolute paths
This commit is contained in:
parent
7c08e1fbe0
commit
499c0d8568
|
|
@ -14,11 +14,16 @@ export default class PosteditController extends Controller {
|
|||
async save(ev) {
|
||||
ev.preventDefault();
|
||||
|
||||
showToast({
|
||||
title: "💾 Saving Post",
|
||||
body: (this.saveActionValue === "Save Draft") ? "Saving post as draft…" : "Updating post…",
|
||||
});
|
||||
|
||||
try {
|
||||
await this._postForm(this.saveActionValue);
|
||||
|
||||
showToast({
|
||||
title: "💾 Post Saved",
|
||||
title: "💾 Saving Post",
|
||||
body: (this.saveActionValue === "Save Draft") ? "Post saved as draft." : "Post updated.",
|
||||
});
|
||||
} catch (e) {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/gopherlibs/feedhub/feedhub"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
|
|
@ -20,10 +21,11 @@ import (
|
|||
)
|
||||
|
||||
type Builder struct {
|
||||
site pubmodel.Site
|
||||
mdRenderer *markdown.Renderer
|
||||
opts Options
|
||||
tmpls *template.Template
|
||||
site pubmodel.Site
|
||||
mdRenderer *markdown.Renderer
|
||||
opts Options
|
||||
tmpls *template.Template
|
||||
postMDProcessors []postMDProcessor
|
||||
}
|
||||
|
||||
func New(site pubmodel.Site, opts Options) (*Builder, error) {
|
||||
|
|
@ -39,6 +41,9 @@ func New(site pubmodel.Site, opts Options) (*Builder, error) {
|
|||
opts: opts,
|
||||
tmpls: tmpls,
|
||||
mdRenderer: markdown.NewRendererForSite(),
|
||||
postMDProcessors: []postMDProcessor{
|
||||
uploadAbsoluteURL,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -207,6 +212,26 @@ func (b *Builder) renderPost(post *models.Post) (postSingleData, error) {
|
|||
return postSingleData{}, fmt.Errorf("failed to write post %s: %w", post.Slug, err)
|
||||
}
|
||||
|
||||
if len(b.postMDProcessors) > 0 {
|
||||
dom, err := goquery.NewDocumentFromReader(&md)
|
||||
if err != nil {
|
||||
return postSingleData{}, fmt.Errorf("failed to parse post %s: %w", post.Slug, err)
|
||||
}
|
||||
|
||||
for _, processor := range b.postMDProcessors {
|
||||
if err := processor(b.site, dom); err != nil {
|
||||
return postSingleData{}, fmt.Errorf("failed to process post %s: %w", post.Slug, err)
|
||||
}
|
||||
}
|
||||
|
||||
outHTML, err := dom.Find("body").Html()
|
||||
if err != nil {
|
||||
return postSingleData{}, fmt.Errorf("failed to render post %s: %w", post.Slug, err)
|
||||
}
|
||||
md.Reset()
|
||||
md.WriteString(outHTML)
|
||||
}
|
||||
|
||||
postURL := strings.TrimSuffix(b.site.BaseURL, "/") + "/" + strings.TrimPrefix(postPath, "/")
|
||||
|
||||
return postSingleData{
|
||||
|
|
|
|||
37
providers/sitebuilder/processors.go
Normal file
37
providers/sitebuilder/processors.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package sitebuilder
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"lmika.dev/lmika/weiro/models/pubmodel"
|
||||
)
|
||||
|
||||
type postMDProcessor func(site pubmodel.Site, dom *goquery.Document) error
|
||||
|
||||
func uploadAbsoluteURL(site pubmodel.Site, dom *goquery.Document) error {
|
||||
siteURL, err := url.Parse(site.BaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dom.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||
srcUrl := s.AttrOr("src", "")
|
||||
if site.BaseURL == "" {
|
||||
return
|
||||
} else if strings.HasPrefix(srcUrl, "http:") || strings.HasPrefix(srcUrl, "https:") {
|
||||
return
|
||||
}
|
||||
|
||||
pu, err := url.Parse(srcUrl)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
absURL := siteURL.ResolveReference(pu)
|
||||
|
||||
s.SetAttr("src", absURL.String())
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ package sitebuilder
|
|||
import (
|
||||
"html/template"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models/pubmodel"
|
||||
|
|
@ -20,7 +20,7 @@ func templateFns(site pubmodel.Site, opts Options) template.FuncMap {
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pu.Path = filepath.Join(pu.Path, basePath)
|
||||
pu.Path = joinPath(pu.Path, basePath)
|
||||
return pu.String(), nil
|
||||
},
|
||||
"format_date": func(date time.Time) string {
|
||||
|
|
@ -32,3 +32,7 @@ func templateFns(site pubmodel.Site, opts Options) template.FuncMap {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
func joinPath(basePath, path string) string {
|
||||
return strings.TrimSuffix(basePath, "/") + "/" + strings.TrimPrefix(path, "/")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue