Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support markdown descriptions #18

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: support markdown descriptions
  • Loading branch information
twelvelabs committed Dec 17, 2023
commit 12fa12488f1f15a132b7fa5117f889a879bd1b84
4 changes: 1 addition & 3 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ MD013:
tables: false

# MD033/no-inline-html - Inline HTML
MD033:
allowed_elements:
- br
MD033: false
1 change: 1 addition & 0 deletions .markdownlintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
internal/jsonschema/templates/markdown.tpl.md
internal/markdown/testdata
7 changes: 6 additions & 1 deletion bin/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env bash
set -o errexit -o errtrace -o nounset -o pipefail

update=""
if [[ "${UPDATE:-}" != "" ]]; then
update="-update"
fi

go mod tidy
go test --coverprofile=coverage.out ./...
go test --coverprofile=coverage.out ./... "${update}"
2 changes: 2 additions & 0 deletions cspell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ words:
- gojsonpointer
- golangci
- golangci-lint
- goldmark
- gomod
- goreleaser
- gpgconf
Expand All @@ -59,6 +60,7 @@ words:
- jsonschema
- mapset
- markdownlint
- minifier
- nestif
- nolint
- noreply
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ require (
github.com/gobuffalo/flect v1.0.2
github.com/muesli/mango-cobra v1.2.0
github.com/muesli/roff v0.1.0
github.com/sebdah/goldie/v2 v2.5.3
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/tdewolff/minify/v2 v2.20.9
github.com/twelvelabs/termite v0.13.1
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb
github.com/yuin/goldmark v1.6.0
)

require (
Expand Down Expand Up @@ -50,12 +53,14 @@ require (
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/tdewolff/parse/v2 v2.7.6 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
27 changes: 24 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
Expand Down Expand Up @@ -95,6 +98,7 @@ github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8=
github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
Expand All @@ -104,6 +108,11 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand All @@ -118,6 +127,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
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.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand All @@ -126,11 +137,19 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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/tdewolff/minify/v2 v2.20.9 h1:0RGsL+jBpm77obkuNCjNZ2eiN81CZzTnjeVmTqxCmYk=
github.com/tdewolff/minify/v2 v2.20.9/go.mod h1:hZnNtFqXVQ5QIAR05tdgvS7h6E80jyRwHSGVmM4jbzQ=
github.com/tdewolff/parse/v2 v2.7.6 h1:PGZH2b/itDSye9RatReRn4GBhsT+KFEMtAMjHRuY1h8=
github.com/tdewolff/parse/v2 v2.7.6/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52 h1:gAQliwn+zJrkjAHVcBEYW/RFvd2St4yYimisvozAYlA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/twelvelabs/termite v0.13.1 h1:e5m67QY7MKJ7z7e3a59pQTNowptAL4FTCw7xhnPvhto=
github.com/twelvelabs/termite v0.13.1/go.mod h1:2tjVjIRrhqyptjCeevwyNMN+mx25NS5lD1bxb9ka8JA=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.6.0 h1:boZcn2GTjpsynOsC0iJHnBWa4Bi0qzfJjthwauItG68=
github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
Expand All @@ -156,8 +175,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand All @@ -173,9 +192,11 @@ 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/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 5 additions & 0 deletions internal/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ import (

"github.com/twelvelabs/schemadoc/internal/core"
"github.com/twelvelabs/schemadoc/internal/jsonschema"
"github.com/twelvelabs/schemadoc/internal/markdown"
)

func init() {
render.FuncMap["toHTML"] = markdown.ToHTMLString
}

func NewGenCmd(app *core.App) *cobra.Command {
a := &GenAction{
App: app,
Expand Down
40 changes: 40 additions & 0 deletions internal/jsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Schema struct {
Description string `json:"description,omitempty"`
Else *Schema `json:"else,omitempty"`
Enum []Any `json:"enum,omitempty"`
EnumDescriptions []string `json:"enumDescriptions,omitempty"`
Examples []Any `json:"examples,omitempty"`
ExclusiveMaximum float64 `json:"exclusiveMaximum,omitempty"`
ExclusiveMinimum float64 `json:"exclusiveMinimum,omitempty"`
Expand Down Expand Up @@ -164,6 +165,43 @@ func (s *Schema) EnsureDocument() {
}
}

// EnumMarkdown returns the enum and enum descriptions
// formatted as a markdown list.
func (s *Schema) EnumMarkdown() string {
items := []string{}

for idx, enum := range s.Enum {
desc := ""
if idx < len(s.EnumDescriptions) {
desc = s.EnumDescriptions[idx]
}
item := " * `" + enum.String() + "`"
if desc != "" {
item += ": " + desc
}
items = append(items, item)
}

if len(items) == 0 {
return ""
}
return strings.Join(items, "\n")
}

// ExamplesMarkdown returns the examples as a markdown list.
func (s *Schema) ExamplesMarkdown() string {
items := []string{}

for _, example := range s.Examples {
items = append(items, fmt.Sprintf(" * `%s`", example.String()))
}

if len(items) == 0 {
return ""
}
return strings.Join(items, "\n")
}

// Merge merges fields from other into the receiver.
func (s *Schema) Merge(other *Schema) {
s.EnsureDocument()
Expand Down Expand Up @@ -205,6 +243,8 @@ func (s *Schema) Merge(other *Schema) {
s.Else = other.Else
case "enum":
s.Enum = other.Enum
case "enumDescriptions":
s.EnumDescriptions = other.EnumDescriptions
case "examples":
s.Examples = other.Examples
case "exclusiveMaximum":
Expand Down
43 changes: 43 additions & 0 deletions internal/jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,48 @@ func TestSchema_EntityName(t *testing.T) {
require.Equal("Bar", schema.EntityName())
}

func TestSchema_EnumMarkdown(t *testing.T) {
require := require.New(t)

schema := Schema{}
require.Equal("", schema.EnumMarkdown())

schema = Schema{
Enum: []Any{
{"one"},
{"two"},
},
}
require.Equal(" * `one`\n * `two`", schema.EnumMarkdown())

schema = Schema{
Enum: []Any{
{"one"},
{"two"},
},
EnumDescriptions: []string{
"the first number",
"the second number",
},
}
require.Equal(" * `one`: the first number\n * `two`: the second number", schema.EnumMarkdown())
}

func TestSchema_ExamplesMarkdown(t *testing.T) {
require := require.New(t)

schema := Schema{}
require.Equal("", schema.ExamplesMarkdown())

schema = Schema{
Examples: []Any{
{"example one"},
{"example two"},
},
}
require.Equal(" * `example one`\n * `example two`", schema.ExamplesMarkdown())
}

func TestSchema_GenPath(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -202,6 +244,7 @@ func TestSchema_Merge(t *testing.T) {
"description": true,
"else": true,
"enum": true,
"enumDescriptions": true,
"examples": true,
"exclusiveMaximum": true,
"exclusiveMinimum": true,
Expand Down
12 changes: 6 additions & 6 deletions internal/jsonschema/templates/markdown.tpl.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{ define "ConstTpl" -}}
{{ if .String }}<br><br>Allowed value:<br>• {{ .String }}{{ end -}}
{{ if .String }}<p>Allowed value:</p><ul><li><code>{{ .String }}</code></li></ul>{{ end -}}
{{ end -}}

{{ define "EnumTpl" -}}
{{ if . }}<br><br>Allowed values:<br>
{{- $items := list -}}
{{- range $e := . }}{{ $items = cat "•" $e.String | append $items }}{{ end -}}
{{- $items | join "<br>" -}}
{{ if . }}<p>Allowed values:</p>{{ . | toHTML }}{{ end -}}
{{ end -}}

{{ define "ExamplesTpl" -}}
{{ if . }}<p>Examples:</p>{{ . | toHTML }}{{ end -}}
{{ end -}}

{{ define "PropertiesTpl" -}}
Expand All @@ -16,7 +16,7 @@
| -------- | ---- | -------- | ------- | ----------- |
{{ $propParent := . -}}
{{ range $key, $prop := $propParent.Properties -}}
| `{{ $key }}` | {{ $prop.TypeInfoMarkdown }} | {{ if $propParent.RequiredKey $key }}✅{{ end }} | {{ $prop.Default }} | {{ $prop.Description }}{{ template "ConstTpl" $prop.Const }}{{ template "EnumTpl" $prop.Enum }} |
| `{{ $key }}` | {{ $prop.TypeInfoMarkdown }} | {{ if $propParent.RequiredKey $key }}✅{{ end }} | {{ $prop.Default }} | {{ $prop.Description | toHTML }}{{ template "ConstTpl" $prop.Const }}{{ template "EnumTpl" $prop.EnumMarkdown }}{{ template "ExamplesTpl" $prop.ExamplesMarkdown }} |
{{ end }}
{{ end -}}
{{ end -}}
Expand Down
60 changes: 60 additions & 0 deletions internal/markdown/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package markdown

import (
"bytes"
"fmt"

"github.com/tdewolff/minify/v2"
mhtml "github.com/tdewolff/minify/v2/html"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)

func newConverter() goldmark.Markdown {
return goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
// html.WithHardWraps(),
html.WithXHTML(),
),
)
}

func newMinifier() *minify.M {
// cspell: words mhtml
m := minify.New()
m.AddFunc("text/html", mhtml.Minify)
m.Add("text/html", &mhtml.Minifier{
KeepQuotes: true,
})
return m
}

// ToHTMLBytes converts the given byte array to HTML.
func ToHTMLBytes(markdown []byte) ([]byte, error) {
converted := &bytes.Buffer{}
if err := newConverter().Convert(markdown, converted); err != nil {
return nil, fmt.Errorf("convert error: %w", err)
}

Check warning on line 46 in internal/markdown/markdown.go

View check run for this annotation

Codecov / codecov/patch

internal/markdown/markdown.go#L45-L46

Added lines #L45 - L46 were not covered by tests
minified := &bytes.Buffer{}
if err := newMinifier().Minify("text/html", minified, converted); err != nil {
return nil, fmt.Errorf("minify error: %w", err)
}

Check warning on line 50 in internal/markdown/markdown.go

View check run for this annotation

Codecov / codecov/patch

internal/markdown/markdown.go#L49-L50

Added lines #L49 - L50 were not covered by tests
b := minified.Bytes()
b = bytes.ReplaceAll(b, []byte("\n"), []byte(" "))
b = bytes.ReplaceAll(b, []byte("|"), []byte("&#124;"))
return b, nil
}

func ToHTMLString(markdown string) (string, error) {
buf, err := ToHTMLBytes([]byte(markdown))
return string(buf), err
}
Loading