Move convert function to models
This commit is contained in:
parent
aa6aef939d
commit
e92c41835d
|
@ -2,3 +2,9 @@ module jack
|
|||
|
||||
go 1.18
|
||||
|
||||
require github.com/yuin/goldmark v1.4.14
|
||||
|
||||
require (
|
||||
github.com/yuin/goldmark-meta v1.1.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
)
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
github.com/yuin/goldmark v1.4.14 h1:jwww1XQfhJN7Zm+/a1ZA/3WUiEBEroYFNTiV3dKwM8U=
|
||||
github.com/yuin/goldmark v1.4.14/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
|
||||
github.com/yuin/goldmark-meta v1.1.0/go.mod h1:U4spWENafuA7Zyg+Lj5RqK/MF+ovMYtBvXi1lBb2VP0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
41
main.go
41
main.go
|
@ -1,17 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"jack/models"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/renderer/html"
|
||||
)
|
||||
|
||||
func walk(rootDir string) ([]string, error) {
|
||||
|
@ -33,32 +28,6 @@ func walk(rootDir string) ([]string, error) {
|
|||
return files, err
|
||||
}
|
||||
|
||||
func convertMarkdownToHtml(file string) {
|
||||
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
md := goldmark.New(
|
||||
goldmark.WithExtensions(extension.GFM),
|
||||
goldmark.WithParserOptions(
|
||||
parser.WithAutoHeadingID(),
|
||||
),
|
||||
goldmark.WithRendererOptions(
|
||||
html.WithHardWraps(),
|
||||
html.WithXHTML(),
|
||||
),
|
||||
)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := md.Convert(content, &buf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println(buf.String())
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var rootDir *string
|
||||
|
@ -73,7 +42,7 @@ func main() {
|
|||
outputDir = flag.String("out", defaultOutputDir, "Specify the output directory")
|
||||
flag.Usage = func() {
|
||||
fmt.Printf("Jack expects: \n")
|
||||
fmt.Printf("./jack -d <rootDir>\n")
|
||||
fmt.Printf("./jack -dir <rootDir>\n")
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
|
@ -81,7 +50,11 @@ func main() {
|
|||
|
||||
for _, file := range files {
|
||||
fmt.Println(file)
|
||||
convertMarkdownToHtml(file)
|
||||
article := models.Article{
|
||||
File: file,
|
||||
}
|
||||
article.ReadFile()
|
||||
fmt.Println(article)
|
||||
}
|
||||
|
||||
fmt.Println(*outputDir)
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
meta "github.com/yuin/goldmark-meta"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/renderer/html"
|
||||
)
|
||||
|
||||
type Article struct {
|
||||
Title string
|
||||
HtmlContent string
|
||||
publishedDate string
|
||||
Url string
|
||||
File string
|
||||
}
|
||||
|
||||
func (a Article) PublishedDate() {
|
||||
var t time.Time
|
||||
fmt.Println(t)
|
||||
}
|
||||
|
||||
func (a Article) ReadFile() Article {
|
||||
|
||||
content, err := os.ReadFile(a.File)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
md := goldmark.New(
|
||||
goldmark.WithExtensions(extension.GFM, meta.Meta),
|
||||
goldmark.WithParserOptions(
|
||||
parser.WithAutoHeadingID(),
|
||||
),
|
||||
goldmark.WithRendererOptions(
|
||||
html.WithHardWraps(),
|
||||
html.WithXHTML(),
|
||||
),
|
||||
)
|
||||
|
||||
var buf bytes.Buffer
|
||||
context := parser.NewContext()
|
||||
if err := md.Convert(content, &buf, parser.WithContext(context)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
metadata := meta.Get(context)
|
||||
a.Title, _ = metadata["Title"].(string)
|
||||
a.HtmlContent = buf.String()
|
||||
|
||||
fmt.Println(a.Title)
|
||||
fmt.Println(a.HtmlContent)
|
||||
|
||||
return a
|
||||
}
|
Loading…
Reference in New Issue