diff --git a/README.md b/README.md index 5f59c8f..0c9d17c 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,7 @@ You can override autolinking patterns via options. | Functional option | Type | Description | | ----------------- | ---- | ----------- | -| `extension.WithLinkifyAllowedProtocols` | `[][]byte` | List of allowed protocols such as `[][]byte{ []byte("http:") }` | +| `extension.WithLinkifyAllowedProtocols` | `[][]byte \| []string` | List of allowed protocols such as `[]string{ "http:" }` | | `extension.WithLinkifyURLRegexp` | `*regexp.Regexp` | Regexp that defines URLs, including protocols | | `extension.WithLinkifyWWWRegexp` | `*regexp.Regexp` | Regexp that defines URL starting with `www.`. This pattern corresponds to [the extended www autolink](https://github.github.com/gfm/#extended-www-autolink) | | `extension.WithLinkifyEmailRegexp` | `*regexp.Regexp` | Regexp that defines email addresses` | @@ -277,9 +277,9 @@ markdown := goldmark.New( ), goldmark.WithExtensions( extension.NewLinkify( - extension.WithLinkifyAllowedProtocols([][]byte{ - []byte("http:"), - []byte("https:"), + extension.WithLinkifyAllowedProtocols([]string{ + "http:", + "https:", }), extension.WithLinkifyURLRegexp( xurls.Strict, @@ -297,13 +297,13 @@ This extension has some options: | Functional option | Type | Description | | ----------------- | ---- | ----------- | -| `extension.WithFootnoteIDPrefix` | `[]byte` | a prefix for the id attributes.| +| `extension.WithFootnoteIDPrefix` | `[]byte \| string` | a prefix for the id attributes.| | `extension.WithFootnoteIDPrefixFunction` | `func(gast.Node) []byte` | a function that determines the id attribute for given Node.| -| `extension.WithFootnoteLinkTitle` | `[]byte` | an optional title attribute for footnote links.| -| `extension.WithFootnoteBacklinkTitle` | `[]byte` | an optional title attribute for footnote backlinks. | -| `extension.WithFootnoteLinkClass` | `[]byte` | a class for footnote links. This defaults to `footnote-ref`. | -| `extension.WithFootnoteBacklinkClass` | `[]byte` | a class for footnote backlinks. This defaults to `footnote-backref`. | -| `extension.WithFootnoteBacklinkHTML` | `[]byte` | a class for footnote backlinks. This defaults to `↩︎`. | +| `extension.WithFootnoteLinkTitle` | `[]byte \| string` | an optional title attribute for footnote links.| +| `extension.WithFootnoteBacklinkTitle` | `[]byte \| string` | an optional title attribute for footnote backlinks. | +| `extension.WithFootnoteLinkClass` | `[]byte \| string` | a class for footnote links. This defaults to `footnote-ref`. | +| `extension.WithFootnoteBacklinkClass` | `[]byte \| string` | a class for footnote backlinks. This defaults to `footnote-backref`. | +| `extension.WithFootnoteBacklinkHTML` | `[]byte \| string` | a class for footnote backlinks. This defaults to `↩︎`. | Some options can have special substitutions. Occurrences of “^^” in the string will be replaced by the corresponding footnote number in the HTML output. Occurrences of “%%” will be replaced by a number for the reference (footnotes can have multiple references). @@ -319,7 +319,7 @@ for _, path := range files { markdown := goldmark.New( goldmark.WithExtensions( NewFootnote( - WithFootnoteIDPrefix([]byte(path)), + WithFootnoteIDPrefix(path), ), ), ) @@ -379,7 +379,7 @@ This extension provides additional options for CJK users. | Functional option | Type | Description | | ----------------- | ---- | ----------- | -| `extension.WithEastAsianLineBreaks` | `...extension.EastAsianLineBreaksStyle` | Soft line breaks are rendered as a newline. Some asian users will see it as an unnecessary space. With this option, soft line breaks between east asian wide characters will be ignored. | +| `extension.WithEastAsianLineBreaks` | `...extension.EastAsianLineBreaksStyle` | Soft line breaks are rendered as a newline. Some asian users will see it as an unnecessary space. With this option, soft line breaks between east asian wide characters will be ignored. This defaults to `EastAsianLineBreaksStyleSimple`. | | `extension.WithEscapedSpace` | `-` | Without spaces around an emphasis started with east asian punctuations, it is not interpreted as an emphasis(as defined in CommonMark spec). With this option, you can avoid this inconvenient behavior by putting 'not rendered' spaces around an emphasis like `太郎は\ **「こんにちわ」**\ といった`. | #### Styles of Line Breaking diff --git a/extension/footnote.go b/extension/footnote.go index d1b67aa..2e22526 100644 --- a/extension/footnote.go +++ b/extension/footnote.go @@ -382,8 +382,8 @@ func (o *withFootnoteIDPrefix) SetFootnoteOption(c *FootnoteConfig) { } // WithFootnoteIDPrefix is a functional option that is a prefix for the id attributes generated by footnotes. -func WithFootnoteIDPrefix(a []byte) FootnoteOption { - return &withFootnoteIDPrefix{a} +func WithFootnoteIDPrefix[T []byte | string](a T) FootnoteOption { + return &withFootnoteIDPrefix{[]byte(a)} } const optFootnoteIDPrefixFunction renderer.OptionName = "FootnoteIDPrefixFunction" @@ -420,8 +420,8 @@ func (o *withFootnoteLinkTitle) SetFootnoteOption(c *FootnoteConfig) { } // WithFootnoteLinkTitle is a functional option that is an optional title attribute for footnote links. -func WithFootnoteLinkTitle(a []byte) FootnoteOption { - return &withFootnoteLinkTitle{a} +func WithFootnoteLinkTitle[T []byte | string](a T) FootnoteOption { + return &withFootnoteLinkTitle{[]byte(a)} } const optFootnoteBacklinkTitle renderer.OptionName = "FootnoteBacklinkTitle" @@ -439,8 +439,8 @@ func (o *withFootnoteBacklinkTitle) SetFootnoteOption(c *FootnoteConfig) { } // WithFootnoteBacklinkTitle is a functional option that is an optional title attribute for footnote backlinks. -func WithFootnoteBacklinkTitle(a []byte) FootnoteOption { - return &withFootnoteBacklinkTitle{a} +func WithFootnoteBacklinkTitle[T []byte | string](a T) FootnoteOption { + return &withFootnoteBacklinkTitle{[]byte(a)} } const optFootnoteLinkClass renderer.OptionName = "FootnoteLinkClass" @@ -458,8 +458,8 @@ func (o *withFootnoteLinkClass) SetFootnoteOption(c *FootnoteConfig) { } // WithFootnoteLinkClass is a functional option that is a class for footnote links. -func WithFootnoteLinkClass(a []byte) FootnoteOption { - return &withFootnoteLinkClass{a} +func WithFootnoteLinkClass[T []byte | string](a T) FootnoteOption { + return &withFootnoteLinkClass{[]byte(a)} } const optFootnoteBacklinkClass renderer.OptionName = "FootnoteBacklinkClass" @@ -477,8 +477,8 @@ func (o *withFootnoteBacklinkClass) SetFootnoteOption(c *FootnoteConfig) { } // WithFootnoteBacklinkClass is a functional option that is a class for footnote backlinks. -func WithFootnoteBacklinkClass(a []byte) FootnoteOption { - return &withFootnoteBacklinkClass{a} +func WithFootnoteBacklinkClass[T []byte | string](a T) FootnoteOption { + return &withFootnoteBacklinkClass{[]byte(a)} } const optFootnoteBacklinkHTML renderer.OptionName = "FootnoteBacklinkHTML" @@ -496,8 +496,8 @@ func (o *withFootnoteBacklinkHTML) SetFootnoteOption(c *FootnoteConfig) { } // WithFootnoteBacklinkHTML is an HTML content for footnote backlinks. -func WithFootnoteBacklinkHTML(a []byte) FootnoteOption { - return &withFootnoteBacklinkHTML{a} +func WithFootnoteBacklinkHTML[T []byte | string](a T) FootnoteOption { + return &withFootnoteBacklinkHTML{[]byte(a)} } // FootnoteHTMLRenderer is a renderer.NodeRenderer implementation that diff --git a/extension/footnote_test.go b/extension/footnote_test.go index 54af1be..af22443 100644 --- a/extension/footnote_test.go +++ b/extension/footnote_test.go @@ -38,12 +38,12 @@ func TestFootnoteOptions(t *testing.T) { ), goldmark.WithExtensions( NewFootnote( - WithFootnoteIDPrefix([]byte("article12-")), - WithFootnoteLinkClass([]byte("link-class")), - WithFootnoteBacklinkClass([]byte("backlink-class")), - WithFootnoteLinkTitle([]byte("link-title-%%-^^")), - WithFootnoteBacklinkTitle([]byte("backlink-title")), - WithFootnoteBacklinkHTML([]byte("^")), + WithFootnoteIDPrefix("article12-"), + WithFootnoteLinkClass("link-class"), + WithFootnoteBacklinkClass("backlink-class"), + WithFootnoteLinkTitle("link-title-%%-^^"), + WithFootnoteBacklinkTitle("backlink-title"), + WithFootnoteBacklinkHTML("^"), ), ), ) diff --git a/extension/linkify.go b/extension/linkify.go index 0f23e90..ad88933 100644 --- a/extension/linkify.go +++ b/extension/linkify.go @@ -66,10 +66,12 @@ func (o *withLinkifyAllowedProtocols) SetLinkifyOption(p *LinkifyConfig) { // WithLinkifyAllowedProtocols is a functional option that specify allowed // protocols in autolinks. Each protocol must end with ':' like // 'http:' . -func WithLinkifyAllowedProtocols(value [][]byte) LinkifyOption { - return &withLinkifyAllowedProtocols{ - value: value, +func WithLinkifyAllowedProtocols[T []byte | string](value []T) LinkifyOption { + opt := &withLinkifyAllowedProtocols{} + for _, v := range value { + opt.value = append(opt.value, []byte(v)) } + return opt } type withLinkifyURLRegexp struct { diff --git a/extension/linkify_test.go b/extension/linkify_test.go index d096aca..4d70ea4 100644 --- a/extension/linkify_test.go +++ b/extension/linkify_test.go @@ -29,8 +29,8 @@ func TestLinkifyWithAllowedProtocols(t *testing.T) { ), goldmark.WithExtensions( NewLinkify( - WithLinkifyAllowedProtocols([][]byte{ - []byte("ssh:"), + WithLinkifyAllowedProtocols([]string{ + "ssh:", }), WithLinkifyURLRegexp( regexp.MustCompile(`\w+://[^\s]+`), diff --git a/extension/typographer.go b/extension/typographer.go index 259c4f7..44c15eb 100644 --- a/extension/typographer.go +++ b/extension/typographer.go @@ -115,10 +115,10 @@ func (o *withTypographicSubstitutions) SetTypographerOption(p *TypographerConfig // WithTypographicSubstitutions is a functional otpion that specify replacement text // for punctuations. -func WithTypographicSubstitutions(values map[TypographicPunctuation][]byte) TypographerOption { +func WithTypographicSubstitutions[T []byte | string](values map[TypographicPunctuation]T) TypographerOption { replacements := newDefaultSubstitutions() for k, v := range values { - replacements[k] = v + replacements[k] = []byte(v) } return &withTypographicSubstitutions{replacements} diff --git a/go.mod b/go.mod index 3a2e0db..dd98e31 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/yuin/goldmark -go 1.18 +go 1.19