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

Markdown footnotes prefix (per page) #10932

Open
marcellmars opened this issue May 8, 2023 · 0 comments
Open

Markdown footnotes prefix (per page) #10932

marcellmars opened this issue May 8, 2023 · 0 comments

Comments

@marcellmars
Copy link

I use footnotes in markdown files. With excellent library paged.js I compile a number of markdown files one after another and get the well paginated PDF.

There are few UI details which need extra care in this kind of setup.

One is prefix counters of headers and subheaders and for that I used a Markdown header hooks, in particular: heading hook.

I am interested to hear what are the different ways how the problem of distinguishing/resetting footnotes could be solved. My approach was to add to Hugo something already existing in goldmark: WithFootnoteIDPrefixFunction

The footnote prefix adds converter.DocumentContext{Document ID} which should be the page's UniqueID coming from source.File if it finds following line in config.toml:

[markup.goldmark.extensions]
footnoteprefix = true

I know this is most probably out of scope of Hugo's core module but I'll be brave enough to paste the diff here:

modified   markup/goldmark/convert.go
@@ -34,6 +34,7 @@ import (
 	"github.com/yuin/goldmark/renderer"
 	"github.com/yuin/goldmark/renderer/html"
 	"github.com/yuin/goldmark/text"
+	"github.com/yuin/goldmark/util"
 )
 
 const (
@@ -133,7 +134,20 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown {
 	}
 
 	if cfg.Extensions.Footnote {
-		extensions = append(extensions, extension.Footnote)
+		if cfg.Extensions.FootnotePrefix {
+			f := extension.NewFootnote(
+				extension.WithFootnoteIDPrefixFunction(func(n ast.Node) []byte {
+					v, ok := n.OwnerDocument().Meta()["footnote-prefix"]
+					if ok {
+						return util.StringToReadOnlyBytes(v.(string))
+					}
+					return nil
+				}),
+			)
+			extensions = append(extensions, f)
+		} else {
+			extensions = append(extensions, extension.Footnote)
+		}
 	}
 
 	if cfg.Parser.AutoHeadingID {
@@ -207,6 +221,10 @@ func (c *goldmarkConverter) Parse(ctx converter.RenderContext) (converter.Result
 		parser.WithContext(pctx),
 	)
 
+	if c.cfg.MarkupConfig.Goldmark.Extensions.FootnotePrefix {
+		doc.OwnerDocument().Meta()["footnote-prefix"] = c.ctx.DocumentID
+	}
+
 	return parserResult{
 		doc: doc,
 		toc: pctx.TableOfContents(),
modified   markup/goldmark/goldmark_config/config.go
@@ -37,6 +37,7 @@ var Default = Config{
 			Apostrophe:       "’",
 		},
 		Footnote:        true,
+		FootnotePrefix:  false,
 		DefinitionList:  true,
 		Table:           true,
 		Strikethrough:   true,
@@ -68,6 +69,7 @@ type Config struct {
 type Extensions struct {
 	Typographer    Typographer
 	Footnote       bool
+	FootnotePrefix bool
 	DefinitionList bool
 
 	// GitHub flavored markdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant