Skip to content

Commit

Permalink
import: fix video/audio parsing from markdown to html
Browse files Browse the repository at this point in the history
  • Loading branch information
hayzamjs committed Nov 28, 2024
1 parent 2ca2cdf commit 7c7a093
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mangoumbrella/goldmark-figure v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mangoumbrella/goldmark-figure v1.2.0 h1:T8wf2VAi0e2G2qeJDSHpO4M6GgkLbzYNliwSuozMcko=
github.com/mangoumbrella/goldmark-figure v1.2.0/go.mod h1:iIL+fhdmCQDpE0l/TKtGhokWzIbo5lo/Y2OIAcx6usI=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
Expand Down
29 changes: 23 additions & 6 deletions services/docs_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
figure "github.com/mangoumbrella/goldmark-figure"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
Expand All @@ -22,6 +23,9 @@ func processMarkdown(content, dir string, cfg *config.Config) (string, error) {
gm := goldmark.New(
goldmark.WithRendererOptions(html.WithUnsafe()),
goldmark.WithParserOptions(parser.WithAutoHeadingID()),
goldmark.WithExtensions(
figure.Figure,
),
)

var output bytes.Buffer
Expand Down Expand Up @@ -50,15 +54,28 @@ func processMarkdown(content, dir string, cfg *config.Config) (string, error) {

absPath := filepath.Join(dir, decodedSrc)
file, err := os.Open(absPath)
if err != nil {
return
}
defer file.Close()

if err == nil {
mime := utils.GetContentType(absPath)
mime := utils.GetContentType(absPath)

s3URL, err := UploadToStorage(file, filepath.Base(absPath), mime, cfg)
s3URL, err := UploadToStorage(file, filepath.Base(absPath), mime, cfg)
if err != nil {
return
}

if err == nil {
s.SetAttr("src", s3URL)
}
if strings.HasPrefix(mime, "image/") {
s.SetAttr("src", s3URL)
} else if strings.HasPrefix(mime, "video/") {
s.BeforeHtml(fmt.Sprintf(`<video controls src="%s"></video>`, s3URL))
s.Remove()
} else if strings.HasPrefix(mime, "audio/") {
s.BeforeHtml(fmt.Sprintf(`<audio controls src="%s"></audio>`, s3URL))
s.Remove()
} else {
s.SetAttr("src", s3URL)
}
}
})
Expand Down

0 comments on commit 7c7a093

Please sign in to comment.