Skip to content

How to rewrite links? #346

Answered by abhinav
ivanjaros asked this question in Q&A
Jan 3, 2023 · 3 comments · 3 replies
Discussion options

You must be logged in to vote

You should be able to do this by adding an ASTTransformer to your parser.
Start with something like the following:

tr := &imageLinkRewriter{}
md := goldmark.New(
  goldmark.WithParserOptions(
    parser.WithASTTransformers(util.Prioritized(tr, 100)),
  ),
)

Where imageLinkRewriter is your transformer.
It should satisfy the ASTTransformer interface.
That lets you do anything to the document,
but typical usage involves traversing the document with ast.Walk.

For example,

type imageLinkRewriter struct{}

func (t *imageLinkRewriter) Transform(doc *ast.Document, reader text.Reader, pctx parser.Context) {
  ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
    if !enter {
 …

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ivanjaros
Comment options

Answer selected by ivanjaros
Comment options

You must be logged in to vote
2 replies
@abhinav
Comment options

@ivanjaros
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants