hashtag

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2023 License: MIT Imports: 1 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference Go codecov

goldmark-hashtag is an extension for the goldmark Markdown parser that adds support for tagging documents with #hashtags.

Usage

To use goldmark-hashtag, import the hashtag package.

import hashtag "github.com/abhinav/goldmark-hashtag"

Then include the hashtag.Extender in the list of extensions you build your goldmark.Markdown with.

goldmark.New(
  &hashtag.Extender{},
  // ...
).Convert(src, out)

This alone has little effect besides adding <span class="hashtag">...</span> around hashtags in your Markdown document.

Supply a hashtag.Resolver to the hashtag.Extender to render hashtags as links:

goldmark.New(
  &hashtag.Extender{
    Resolver: hashtagResolver,
  },
  // ...
).Convert(src, out)

Syntax

Hashtags must always begin with a "#". The characters that follow that depend on the variant you have chosen. goldmark-hashtag supports the following variants:

  • Default: Hashtags must begin with a letter, and may contain letters, numbers and any of the following symbols: /_-. goldmark-hashtag uses this variant if you do not specify one.
  • Obsidian: Hashtags can begin with and contain letters, numbers, emoji, and any of the following symbols: /_-, but must not contain only numbers.

You can specify the variant by setting the Variant property of the hashtag.Extender.

&hashtag.Extender{
  // ...
  Variant: hashtag.ObsidianVariant,
}

Inspection

To collect all hashtags from a Markdown document, use Goldmark's ast.Walk function after parsing the document.

For example, the following will populate the hashtags map with all hashtags found in the document.

markdown := goldmark.New(
  &hashtag.Extender{
    Resolver: hashtagResolver,
  },
  // ...
)

// Parse the Markdown document.
doc := markdown.Parser().Parse(text.NewReader(src))

// List the tags.
hashtags := make(map[string]struct{})
ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) {
  if n, ok := node.(*hashtag.Node); ok && enter {
    hashtags[string(n.Tag)] = struct{}{}
  }
  return ast.WalkContinue, nil
})

Documentation

Overview

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

Deprecated: Use go.abhg.dev/goldmark/hashtag instead.

Index

Constants

View Source
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 = hashtag.DefaultVariant

	// 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 = hashtag.ObsidianVariant
)

Variables

View Source
var Kind = hashtag.Kind

Kind is the kind of hashtag AST nodes.

Functions

This section is empty.

Types

type Extender

type Extender = hashtag.Extender

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.

type Node

type Node = hashtag.Node

Node is a hashtag node in a Goldmark Markdown document.

type Parser

type Parser = hashtag.Parser

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.

type Renderer

type Renderer = hashtag.Renderer

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>

type Resolver

type Resolver = hashtag.Resolver

Resolver resolves hashtags to pages they should link to.

type Variant added in v0.2.0

type Variant = hashtag.Variant

Variant represents one of the different flavours of hashtag syntax.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址