package hashtag

import "go.abhg.dev/goldmark/hashtag"

Package hashtag provides support for #tag-style tags for the Goldmark Markdown parser.

Index

Variables

var Kind = ast.NewNodeKind("Hashtag")

Kind is the kind of hashtag AST nodes.

Types

type Extender

type Extender struct {
	// Resolver specifies destination links for hashtags, if any.
	//
	// Defaults to no links.
	Resolver Resolver

	// Variant is the flavor of the hashtag syntax to support.
	//
	// Defaults to DefaultVariant. See the documentation of individual
	// variants for more information.
	Variant Variant
}

Extender extends a goldmark Markdown object with support for parsing and rendering hashtags.

Install it on your Markdown object upon creation.

goldmark.New(
  goldmark.WithExtensions(
    // ...
    &hashtag.Extender{...},
  ),
  // ...
)

Provide a Resolver to render tags as links that point to a specific destination.

func (*Extender) Extend

func (e *Extender) Extend(m goldmark.Markdown)

Extend extends the provided goldmark Markdown object with support for hashtags.

type Node

type Node struct {
	ast.BaseInline

	// Tag is the portion of the hashtag following the '#'.
	Tag []byte
}

Node is a hashtag node in a Goldmark Markdown document.

func (*Node) Dump

func (n *Node) Dump(src []byte, level int)

Dump dumps the contents of Node to stdout for debugging.

func (*Node) Kind

func (*Node) Kind() ast.NodeKind

Kind reports the kind of hashtag nodes.

type Parser

type Parser struct {
	// Variant is the flavor of the hashtag syntax to support.
	//
	// Defaults to DefaultVariant. See the documentation of individual
	// variants for more information.
	Variant Variant
}

Parser is a Goldmark inline parser for parsing hashtag nodes.

Hashtags start with "#". The list of other characters allowed in the hashtag is determined by variant. See the documentation for Variant for more details.

func (*Parser) Parse

func (p *Parser) Parse(parent ast.Node, block text.Reader, pctx parser.Context) ast.Node

Parse parses a hashtag node.

func (*Parser) Trigger

func (*Parser) Trigger() []byte

Trigger reports characters that trigger this parser.

type Renderer

type Renderer struct {
	// Resolver specifies how where hashtag links should point, if at all.
	//
	// When a Resolver returns an empty destination for a hashtag, the
	// Renderer will render the hashtag as plain text rather than a link.
	//
	// Defaults to empty destinations for all hashtags.
	Resolver Resolver
	// contains filtered or unexported fields
}

Renderer renders hashtag nodes into HTML, optionally linking them to specific pages.

#foo

Renders as the following by default.

<span class="hashtag">#foo</span>

Supply a Resolver that returns a non-empty destination to render it like the following.

<span class="hashtag"><a href="...">#foo</a></span>

func (*Renderer) RegisterFuncs

func (r *Renderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)

RegisterFuncs registers rendering functions from this renderer onto the provided registerer.

func (*Renderer) Render

func (r *Renderer) Render(w util.BufWriter, src []byte, node ast.Node, entering bool) (ast.WalkStatus, error)

Render renders a hashtag node as HTML.

type Resolver

type Resolver interface {
	// ResolveHashtag reports the link that the provided hashtag Node
	// should point to, or an empty destination for hashtags that should
	// not link to anything.
	ResolveHashtag(*Node) (destination []byte, err error)
}

Resolver resolves hashtags to pages they should link to.

type Variant

type Variant uint

Variant represents one of the different flavours of hashtag syntax.

const (
	// DefaultVariant is the default flavor of hashtag syntax supported by
	// this package.
	//
	// In this format, hashtags start with "#" and an alphabet, followed by
	// zero or more alphanumeric characters and the following symbols.
	//
	//   /_-
	DefaultVariant Variant = iota

	// ObsidianVariant is a flavor of the hashtag syntax that aims to be
	// compatible with Obsidian (https://obsidian.md/).
	//
	// In this format, hashtags start with "#" followed by alphabets,
	// numbers, emoji, or any of the following symbols.
	//
	//   /_-
	//
	// Hashtags cannot be entirely numeric and must contain at least one
	// non-numeric character.
	//
	// See also https://help.obsidian.md/How+to/Working+with+tags.
	ObsidianVariant
)