Skip to content

Commit

Permalink
生のHTMLや潜在的に危険なリンクをそのままレンダリングするオプションを追加する
Browse files Browse the repository at this point in the history
  • Loading branch information
zztkm committed Sep 25, 2024
1 parent 5a765f7 commit 5f3b077
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
)

// Builder is a struct for building a static site.
Expand Down Expand Up @@ -382,6 +384,7 @@ func (b *Builder) initGoldmark() goldmark.Markdown {
// default extensions
extension.GFM,
}
rendererOptions := []renderer.Option{}
highlightoptions := []highlighting.Option{}
if b.config.Build.Goldmark.HighlightConfig != nil {
if b.config.Build.Goldmark.HighlightConfig.Style != nil {
Expand All @@ -399,8 +402,18 @@ func (b *Builder) initGoldmark() goldmark.Markdown {
if len(highlightoptions) > 0 {
extensions = append(extensions, highlighting.NewHighlighting(highlightoptions...))
}

// renderer options を設定
if b.config.Build.Goldmark.RendererOptions != nil {
if b.config.Build.Goldmark.RendererOptions.WithUnsafe != nil {
if *b.config.Build.Goldmark.RendererOptions.WithUnsafe {
rendererOptions = append(rendererOptions, html.WithUnsafe())
}
}
}
return goldmark.New(
goldmark.WithExtensions(extensions...),
goldmark.WithRendererOptions(rendererOptions...),
)
}

Expand Down
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ type GoldMarkHighlightConfig struct {
WithNumbers *bool `toml:"with_numbers"`
}

type GoldMarkRendererOptions struct {
WithUnsafe *bool `toml:"with_unsafe"`
}

// GoldMarkConfig is a struct for configuring goldmark.
// なぜ Pointer なのか?
// config file にキーを設定したかどうかを判定するため
type GoldMarkConfig struct {
HighlightConfig *GoldMarkHighlightConfig `toml:"highlight"`
RendererOptions *GoldMarkRendererOptions `toml:"renderer"`
}

type BuildConfig struct {
Expand Down

0 comments on commit 5f3b077

Please sign in to comment.