goldmark-pdf
goldmark-pdf copied to clipboard
Local links/navigation is not working
Having local navigation, like:
# <a name="top"></a>Markdown Test Page
* [Headings](#Headings)
* [Paragraphs](#Paragraphs)
* [Blockquotes](#Blockquotes)
* [Lists](#Lists)
* [Horizontal rule](#Horizontal)
* [Table](#Table)
* [Code](#Code)
* [Inline elements](#Inline)
***
# <a name="Headings"></a>Headings
# Heading one
Sint sit cillum pariatur eiusmod nulla pariatur ipsum. Sit laborum anim qui mollit tempor pariatur nisi minim dolor. Aliquip et adipisicing sit sit fugiat commodo id sunt. Nostrud enim ad commodo incididunt cupidatat in ullamco ullamco Lorem cupidatat velit enim et Lorem. Ut laborum cillum laboris fugiat culpa sint irure do reprehenderit culpa occaecat. Exercitation esse mollit tempor magna aliqua in occaecat aliquip veniam reprehenderit nisi dolor in laboris dolore velit.
## Heading two
[[Top]](#top)
will render links but they won't work.
Maybe this will help: https://www.w3.org/WAI/WCAG22/Techniques/pdf/PDF11
specifically:
from gopdf readme
I've added support for local links on the current master branch.
Some things to note:
This only works for hashtag links to headers. The header ID attribute need to be generated by the parser using the goldmark option WithAutoHeadingID
.
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
This means that adding internal links like <a name="here"></a>
or <span id="this"></span>
. Will not work. There are inline HTML fragments which this package cannot render at all.
Upgrade to the most recent commit with go get github.com/stephenafamo/goldmark-pdf@3ab67f1
and try it out.
ok, as long as there is a way. i think it should be mentioned in readme to avoid confusion.
The HTML is working(WithAutoHeadingID simply transliterates the heading into id) but in pdf it does not work.
I am just guessing but maybe in the func (r *nodeRederFuncs) renderHeading
you are adding link to the heading itself and not anchor and then you do not set link to that anchor in link that actually points to it anywhere in the document. The screenshot from readme above clearly shows two requirements - defining anchor on the heading(in this context) and actually linking to it with a link. Since headings are not links, the entire logic does nothing.
in (r *nodeRederFuncs) renderLink
there should be some check for link href starting with "#" and then simply making it into local link.
I am adding the anchors when rendering headings. See here
When rendering links, I am also checking for the #
. See here
Make sure you're on the latest commit, it worked for me. Also, while there is a pending implementation with gopdf
, what is currently used is fpdf
, see fdpf.go
.
my go mod shows v0.2.0 and the code you linked is present but it is not working. I have the list above, just changed the anchors to lower case so they match generated ids and they work in html but not in pdf.
i wish i could somehow debug the pdf code to see what's up.
@ivanjaros I generate with this markdown page.md
And this code
package main
import (
"context"
_ "embed"
"image/color"
"os"
pdf "github.com/stephenafamo/goldmark-pdf"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
)
//go:embed page.md
var markdown []byte
func main() {
file, err := os.Create("./test/output.pdf")
if err != nil {
panic(err)
}
md := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithRenderer(
pdf.New(
pdf.WithContext(context.Background()),
pdf.WithLinkColor(color.RGBA{204, 69, 120, 255}),
pdf.WithHeadingFont(pdf.GetTextFont("IBM Plex Serif", pdf.FontLora)),
pdf.WithBodyFont(pdf.GetTextFont("Open Sans", pdf.FontRoboto)),
pdf.WithCodeFont(pdf.GetCodeFont("Inconsolata", pdf.FontRobotoMono)),
),
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
)
if err := md.Convert(markdown, file); err != nil {
panic(err)
}
}
And get this PDF output.pdf
Thanks, will have a look tomorrow. Most likely some issue on my side.
Nope, still no change. When I click on your pdf's links, they just work but when I generate the pdf without your code and click on my links they not only not work but they will behave like html so the url will change and get a #heading
suffix.
If you added a replace
directive to your go.mod
, you should remove it, if not it will keep using your local version.
If you added a
replace
directive to yourgo.mod
, you should remove it, if not it will keep using your local version.
i did. i tried a fresh project with only this code, same result. i don't know if i am doing something wrong or not.
It works for me.