Skip to content

Commit

Permalink
Merge pull request #17 from vssio/feature/code-highlight
Browse files Browse the repository at this point in the history
Feature/code highlight
  • Loading branch information
zztkm authored Mar 24, 2024
2 parents 38b628e + 7463975 commit e338b35
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 45 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/vssio/go-vss
go 1.21.0

require (
github.com/adrg/frontmatter v0.2.0
github.com/alecthomas/chroma/v2 v2.13.0
github.com/avvmoto/buf-readerat v0.0.0-20171115124131-a17c8cb89270
github.com/bradhe/stopwatch v0.0.0-20190618212248-a58cccc508ea
github.com/cbroglie/mustache v1.4.0
Expand All @@ -11,6 +13,7 @@ require (
github.com/mitchellh/cli v1.1.5
github.com/pelletier/go-toml/v2 v2.1.0
github.com/yuin/goldmark v1.5.6
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
)

require (
Expand All @@ -19,9 +22,9 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/adrg/frontmatter v0.2.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs=
github.com/alecthomas/chroma/v2 v2.13.0 h1:VP72+99Fb2zEcYM0MeaWJmV+xQvz5v5cxRHd+ooU1lI=
github.com/alecthomas/chroma/v2 v2.13.0/go.mod h1:BUGjjsD+ndS6eX37YgTchSEG+Jg9Jv1GiZs9sqPqztk=
github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand All @@ -28,6 +36,10 @@ github.com/cbroglie/mustache v1.4.0/go.mod h1:SS1FTIghy0sjse4DUVGV1k/40B1qE1XkD9
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
Expand All @@ -47,6 +59,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down Expand Up @@ -98,13 +112,17 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.4.15/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.5.6 h1:COmQAWTCcGetChm3Ig7G/t8AFAN00t+o8Mt4cf7JpwA=
github.com/yuin/goldmark v1.5.6/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+IAOyRda+RLrxa1WC7umKOZRsGq4QrFFMYApOeHzQwQ=
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down Expand Up @@ -150,6 +168,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
27 changes: 24 additions & 3 deletions internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"github.com/vssio/go-vss/internal/config"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"

chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
highlighting "github.com/yuin/goldmark-highlighting/v2"
)

// Builder is a struct for building a static site.
Expand All @@ -34,6 +37,16 @@ func NewBuilder(config *config.Config) *Builder {
}
}

// ReloadConfig reloads the config file.
func (b *Builder) ReloadConfig() error {
c, err := config.LoadConfig()
if err != nil {
return err
}
b.config = c
return nil
}

func (b *Builder) Run() error {
if err := createDistDir(b.config.Dist); err != nil {
return err
Expand All @@ -59,7 +72,7 @@ func (b *Builder) Run() error {
}

log.Printf("[INFO] rendering markdown files\n")
b.gm = initGoldmark()
b.gm = b.initGoldmark()
// for storing rendered html
b.baseRenderContext = b.config.AsMap()
for _, markdownPath := range markdownFiles {
Expand Down Expand Up @@ -280,9 +293,17 @@ func getFilePathsByExt(dirPath, ext string) ([]string, error) {
return filePaths, nil
}

func initGoldmark() goldmark.Markdown {
func (b *Builder) initGoldmark() goldmark.Markdown {
return goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithExtensions(
extension.GFM,
highlighting.NewHighlighting(
highlighting.WithStyle(b.config.Build.Goldmark.HighlightConfig.Style),
highlighting.WithFormatOptions(
chromahtml.WithLineNumbers(b.config.Build.Goldmark.HighlightConfig.WithNumbers),
),
),
),
)
}

Expand Down
15 changes: 8 additions & 7 deletions internal/command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bradhe/stopwatch"
"github.com/vssio/go-vss/internal/build"
"github.com/vssio/go-vss/internal/config"
)

type BuildCommand struct {
Expand All @@ -21,22 +22,22 @@ func (c *BuildCommand) Synopsis() string {

func (c *BuildCommand) Run(args []string) int {
log.Println("[INFO] build started")
err := c.Meta.SetupConfig()
// init stop watch
sw := stopwatch.Start()

config, err := config.LoadConfig()
if err != nil {
log.Printf("[ERROR] loading config: %s", err)
log.Printf("[ERROR] %s", err)
return 1
}

// init stop watch
sw := stopwatch.Start()

// TODO: build site
builder := build.NewBuilder(c.Meta.Config)
builder := build.NewBuilder(config)
err = builder.Run()
if err != nil {
log.Printf("[ERROR] %s", err)
return 1
}

sw.Stop()
log.Printf("[INFO] build finished in %v", sw.Milliseconds())
return 0
Expand Down
22 changes: 0 additions & 22 deletions internal/command/meta.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
package command

import (
"github.com/vssio/go-vss/internal/config"
)

// Meta contains the common fields required by all commands.
type Meta struct {
Config *config.Config
Version string
Revision string
}

// SetupConfig initializes the Config field of the Meta struct.
// If the Config field is already initialized, it does nothing.
// NOTE: This method is intended to be called inside a command that requires Config
func (m *Meta) SetupConfig() error {
if m.Config == nil {
var err error
m.Config, err = config.LoadConfig()
if err != nil {
return err
}
m.Config.Dist = "dist"
m.Config.Static = "static"
m.Config.Layouts = "layouts"
}
return nil
}
14 changes: 8 additions & 6 deletions internal/command/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/vssio/go-vss/internal/build"
"github.com/vssio/go-vss/internal/config"
)

const port = "8080"
Expand Down Expand Up @@ -41,17 +42,17 @@ func (c *ServeCommand) Synopsis() string {

func (c *ServeCommand) Run(args []string) int {
log.Printf("[INFO] serve started")
err := c.Meta.SetupConfig()

config, err := config.LoadConfig()
if err != nil {
log.Printf("[ERROR] loading config: %s", err)
log.Printf("[ERROR] %s", err)
return 1
}

// for local server
c.Meta.Config.BaseUrl = "http://localhost:" + port
config.BaseUrl = "http://localhost:" + port

// init site
c.builder = build.NewBuilder(c.Meta.Config)
c.builder = build.NewBuilder(config)
err = c.builder.Run()
if err != nil {
log.Printf("[ERROR] %s", err)
Expand All @@ -61,7 +62,7 @@ func (c *ServeCommand) Run(args []string) int {
// watch for changes
go c.watch()

fs := http.FileServer(htmlDir{http.Dir(c.Meta.Config.Dist)})
fs := http.FileServer(htmlDir{http.Dir(config.Dist)})
http.Handle("/", http.StripPrefix("/", fs))

log.Printf("[INFO] serving on http://localhost:%s\n", port)
Expand Down Expand Up @@ -100,6 +101,7 @@ func (c *ServeCommand) watch() error {
}
if event.Has(fsnotify.Write) {
log.Println("[INFO] modified file:", event.Name)
c.builder.ReloadConfig()
err = c.builder.Run()
if err != nil {
log.Printf("[ERROR] %s", err)
Expand Down
22 changes: 20 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ import (
"github.com/pelletier/go-toml/v2"
)

type GoldMarkHighlightConfig struct {
Style string
WithNumbers bool `toml:"with_numbers"`
}

type GoldMarkConfig struct {
HighlightConfig GoldMarkHighlightConfig `toml:"highlight"`
}

type BuildConfig struct {
IgnoreFiles []string
Goldmark GoldMarkConfig `toml:"goldmark"`
}

type Config struct {
Build BuildConfig
Build BuildConfig `toml:"build"`
Title string
Description string
BaseUrl string `toml:"base_url"`
Expand All @@ -30,7 +40,15 @@ func LoadConfig() (*Config, error) {
if err != nil {
return nil, err
}
return loadConfigFile(path)
c, err := loadConfigFile(path)
if err != nil {
return nil, err
}
// set default values
c.Dist = "dist"
c.Static = "static"
c.Layouts = "layouts"
return c, nil
}

func loadConfigFile(path string) (*Config, error) {
Expand Down
8 changes: 8 additions & 0 deletions site/examples/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Examples

色々な例を紹介していきます。


## Code Highlight

```python
def hello():
print("Hello, World!")
```
4 changes: 0 additions & 4 deletions site/layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<meta name="description" content="{{description}}" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/themes/prism-atom-dark.min.css">
</head>

<body>
Expand Down Expand Up @@ -39,9 +38,6 @@
{{{contents}}}
</div>

<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/components/prism-core.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/plugins/autoloader/prism-autoloader.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-fbbOQedDUMZZ5KreZpsbe1LCZPVmfTnH7ois6mU1QK+m14rQ1l2bGBq41eYeM/fS" crossorigin="anonymous"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions site/vss.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ base_url = "https://vss.veltiosoft.dev/"

[build]
ignore_files = ["README.md"]

[build.goldmark.highlight]
style = "nord"
with_numbers = false

0 comments on commit e338b35

Please sign in to comment.