goldmark-pdf icon indicating copy to clipboard operation
goldmark-pdf copied to clipboard

Local links/navigation is not working

Open ivanjaros opened this issue 1 year ago • 15 comments

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)

obrázok

will render links but they won't work.

ivanjaros avatar Dec 19 '22 16:12 ivanjaros

Maybe this will help: https://www.w3.org/WAI/WCAG22/Techniques/pdf/PDF11 specifically: obrázok

ivanjaros avatar Dec 21 '22 12:12 ivanjaros

from gopdf readme obrázok

ivanjaros avatar Jan 03 '23 20:01 ivanjaros

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.

stephenafamo avatar Jan 06 '23 14:01 stephenafamo

ok, as long as there is a way. i think it should be mentioned in readme to avoid confusion.

ivanjaros avatar Jan 06 '23 15:01 ivanjaros

The HTML is working(WithAutoHeadingID simply transliterates the heading into id) but in pdf it does not work.

ivanjaros avatar Jan 07 '23 09:01 ivanjaros

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.

ivanjaros avatar Jan 07 '23 09:01 ivanjaros

in (r *nodeRederFuncs) renderLink there should be some check for link href starting with "#" and then simply making it into local link.

ivanjaros avatar Jan 07 '23 09:01 ivanjaros

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.

stephenafamo avatar Jan 07 '23 14:01 stephenafamo

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 avatar Jan 07 '23 15:01 ivanjaros

@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

stephenafamo avatar Jan 08 '23 17:01 stephenafamo

Thanks, will have a look tomorrow. Most likely some issue on my side.

ivanjaros avatar Jan 08 '23 20:01 ivanjaros

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.

ivanjaros avatar Jan 09 '23 08:01 ivanjaros

If you added a replace directive to your go.mod, you should remove it, if not it will keep using your local version.

stephenafamo avatar Jan 09 '23 08:01 stephenafamo

If you added a replace directive to your go.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.

ivanjaros avatar Jan 09 '23 09:01 ivanjaros

It works for me.

esnible avatar May 30 '23 22:05 esnible