obsidian

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 22 Imported by: 0

README

goldmark-obsidian

Go Reference CI/CD Coverage Status Go Report Card Release

An Obsidian extension for the goldmark markdown parser.

Features

Obsidian
  • Autolinks (GFM): https://example.com
  • Internal links: [[Link]]
  • Embed files: ![[Link]]
  • Block references: ![[Link#^id]]
  • Defining a block: ^id
  • Comments (block, inline): %%Text%%
  • Strikethroughs (GFM): ~~Text~~
  • Highlights: ==Text==
  • Code blocks: ```
  • Incomplete task (GFM): - [ ]
  • Completed task (GFM): - [x]
  • Callouts: > [!note]
  • Tables (GFM)
  • Footnotes (reference): Example[^1].
  • Footnotes (inline): Example. ^[This is an inline footnote.]
  • Diagrams (Mermaid): ```mermaid
  • LaTeX (MathJax): $$
  • Tags: #tag
  • Properties (metadata): --- at the very beginning of a file

Known inconsistencies with Obsidian:

  • Properties may start and end with at least 1 dash, not exactly 3 (---).
  • Tags defined in properties are not applied to document in same way as other tags.
  • Document aliases defined in properties are not processed as internal link targets.

TODO:

  • Add basic tests for all syntax used in Obsidian Help.
  • Add support for comments (both block and inline).
  • Add support for highlights.
  • Add support for callouts.

Not planned (PR is welcome):

  • Support for inline footnotes.
Obsidian plugin Tasks

TODO:

  • Add an option to support Global Filter (with tag removal).
  • Add an option to set a Default Date.

Not planned (PR is welcome):

  • Dataview Format.

Installation

go get github.com/powerman/goldmark-obsidian

Usage

source := []byte(`
- [ ] Happy New Year 📅 2025-01-01 ^first-task
- [x] Happy Old Year 📅 2024-01-01
`)

md := goldmark.New(
    goldmark.WithExtensions(
        obsidian.NewPlugTasks(),
        obsidian.NewObsidian(),
    ),
)
err := md.Convert(source, os.Stdout)
if err != nil {
    fmt.Println(err)
}
// Output:
// <ul class="contains-task-list">
// <li data-task="" class="task-list-item" id="^first-task"><input disabled="" type="checkbox" class="task-list-item-checkbox"> Happy New Year 📅 2025-01-01</li>
// <li data-task="x" class="task-list-item is-checked"><input checked="" disabled="" type="checkbox" class="task-list-item-checkbox"> Happy Old Year 📅 2024-01-01</li>
// </ul>

Documentation

Overview

Package obsidian provides github.com/yuin/goldmark markdown parser extensions for Obsidian Flavored Markdown and some 3rd-party Obsidian plugins.

Example
package main

import (
	"fmt"
	"os"

	"github.com/yuin/goldmark"

	obsidian "github.com/powerman/goldmark-obsidian"
)

func main() {
	source := []byte(`
- [ ] Happy New Year 📅 2025-01-01 ^first-task
- [x] Happy Old Year 📅 2024-01-01
	`)

	md := goldmark.New(
		goldmark.WithExtensions(
			obsidian.NewPlugTasks(),
			obsidian.NewObsidian(),
		),
	)
	err := md.Convert(source, os.Stdout)
	if err != nil {
		fmt.Println(err)
	}
}
Output:

<ul class="contains-task-list">
<li data-task="" class="task-list-item" id="^first-task"><input disabled="" type="checkbox" class="task-list-item-checkbox"> Happy New Year 📅 2025-01-01</li>
<li data-task="x" class="task-list-item is-checked"><input checked="" disabled="" type="checkbox" class="task-list-item-checkbox"> Happy Old Year 📅 2024-01-01</li>
</ul>

Index

Examples

Constants

View Source
const (
	DefaultPlugTasksListClass               = "contains-task-list"
	DefaultPlugTasksListItemNotCheckedClass = "task-list-item"
	DefaultPlugTasksListItemCheckedClass    = "task-list-item is-checked"
	DefaultPlugTasksListItemStatusAttr      = "data-task"
	DefaultPlugTasksCheckboxClass           = "task-list-item-checkbox"
)

Default attributes used by NewPlugTasksParser.

View Source
const (
	DefaultPlugTasksTODOSymbol       = ' '
	DefaultPlugTasksInProgressSymbol = '/'
	DefaultPlugTasksDoneSymbol       = 'x'
	DefaultPlugTasksCancelledSymbol  = '-'
)

Task status symbols used by NewPlugTasksParser by default. Any other (unknown) symbols will have status TODO.

NOTE: Upper case 'X' is unknown (i.e. has TODO status) in default Tasks configuration.

Variables

View Source
var PlugTasksCheckboxAttributeFilter = html.GlobalAttributeFilter //nolint:gochecknoglobals // By design.

PlugTasksCheckboxAttributeFilter defines attribute names which <input type=checkbox> elements can have.

Functions

This section is empty.

Types

type BlockID

type BlockID struct{}

BlockID is an extension that helps to setup Obsidian block id parser and HTML renderer.

func NewBlockID

func NewBlockID() BlockID

NewBlockID returns a new BlockID extension.

func (BlockID) Extend

func (BlockID) Extend(m goldmark.Markdown)

Extend implements goldmark.Extender.

type BlockIDHTMLRenderer

type BlockIDHTMLRenderer struct{}

BlockIDHTMLRenderer is an HTML renderer for Obsidian block id.

Current implementation does not render block id at all (like Obsidian's "Reading" mode).

func NewBlockIDHTMLRenderer

func NewBlockIDHTMLRenderer() BlockIDHTMLRenderer

NewBlockIDHTMLRenderer returns a new BlockIDHTMLRenderer.

func (BlockIDHTMLRenderer) RegisterFuncs

RegisterFuncs implements renderer.NodeRenderer.

type BlockIDParser

type BlockIDParser struct{}

BlockIDParser is an Obsidian block id parser.

func NewBlockIDParser

func NewBlockIDParser() BlockIDParser

NewBlockIDParser returns a new BlockIDParser.

func (BlockIDParser) Parse

func (BlockIDParser) Parse(parent gast.Node, block text.Reader, pc parser.Context) gast.Node

Parse implements parser.InlineParser.

func (BlockIDParser) Trigger

func (BlockIDParser) Trigger() []byte

Trigger implements parser.InlineParser.

type Obsidian

type Obsidian struct {
	// contains filtered or unexported fields
}

Obsidian is an extension that helps to setup parsers and HTML renderers for Obsidian Flavored Markdown and all other Obsidian features like Footnotes, Diagrams, LaTeX, Tags and Properties.

It's recommended to add other extensions (e.g. Obsidian plugins) before this one to ensure correct parsing priorities. E.g.:

goldmark.WithExtensions(
    obsidian.NewPlugTasks(),
    obsidian.NewObsidian(),
)

func NewObsidian

func NewObsidian() Obsidian

NewObsidian returns a new Obsidian extension.

func (Obsidian) Extend

func (e Obsidian) Extend(m goldmark.Markdown)

Extend implements goldmark.Extender.

func (Obsidian) WithFootnoteOptions

func (e Obsidian) WithFootnoteOptions(opts ...extension.FootnoteOption) Obsidian

WithFootnoteOptions returns a new extension modified to use given opts.

func (Obsidian) WithHashtagResolver

func (e Obsidian) WithHashtagResolver(resolver hashtag.Resolver) Obsidian

WithHashtagResolver returns a new extension modified to use given resolver.

func (Obsidian) WithLinkifyOptions

func (e Obsidian) WithLinkifyOptions(opts ...extension.LinkifyOption) Obsidian

WithLinkifyOptions returns a new extension modified to use given opts.

func (Obsidian) WithMathJaxOptions

func (e Obsidian) WithMathJaxOptions(opts ...mathjax.Option) Obsidian

WithMathJaxOptions returns a new extension modified to use given opts.

func (Obsidian) WithMermaid

func (e Obsidian) WithMermaid(m mermaid.Extender) Obsidian

WithMermaid returns a new extension modified to use given m.

func (Obsidian) WithMetaOptions

func (e Obsidian) WithMetaOptions(opts ...meta.Option) Obsidian

WithMetaOptions returns a new extension modified to use given opts.

func (Obsidian) WithTableOptions

func (e Obsidian) WithTableOptions(opts ...extension.TableOption) Obsidian

WithTableOptions returns a new extension modified to use given opts.

func (Obsidian) WithWikilinkResolver

func (e Obsidian) WithWikilinkResolver(resolver wikilink.Resolver) Obsidian

WithWikilinkResolver returns a new extension modified to use given resolver.

type PlugTasks

type PlugTasks struct {
	// contains filtered or unexported fields
}

PlugTasks is an extension that helps to setup Obsidian plugin Tasks parser and HTML renderer.

func NewPlugTasks

func NewPlugTasks(opts ...PlugTasksOption) PlugTasks

NewPlugTasks returns a new PlugTasks extension.

func (PlugTasks) Extend

func (e PlugTasks) Extend(m goldmark.Markdown)

Extend implements goldmark.Extender.

type PlugTasksHTMLRenderer

type PlugTasksHTMLRenderer struct {
	html.Config // Embed to implement renderer.SetOptioner and apply global html options.
}

PlugTasksHTMLRenderer is an HTML renderer for Obsidian plugin Tasks's tasks.

It renders task status checkbox and task properties in format similar to produced by saving Obsidian plugin Tasks's modal window with task details (add missing spaces, drop invalid properties, etc.).

func NewPlugTasksHTMLRenderer

func NewPlugTasksHTMLRenderer() *PlugTasksHTMLRenderer

NewPlugTasksHTMLRenderer returns a new PlugTasksHTMLRenderer.

func (*PlugTasksHTMLRenderer) RegisterFuncs

RegisterFuncs implements renderer.NodeRenderer.

type PlugTasksOption

type PlugTasksOption func(*plugTasksConfig)

A PlugTasksOption configures a PlugTasksParser.

func WithPlugTasksCheckboxClass

func WithPlugTasksCheckboxClass[T []byte | string](class T) PlugTasksOption

WithPlugTasksCheckboxClass sets a class for an <input type=checkbox>.

func WithPlugTasksListClass

func WithPlugTasksListClass[T []byte | string](class T) PlugTasksOption

WithPlugTasksListClass sets a class for a list containing task(s).

func WithPlugTasksListItemCheckedClass

func WithPlugTasksListItemCheckedClass[T []byte | string](class T) PlugTasksOption

WithPlugTasksListItemCheckedClass sets a class for a list item containing a task with any status symbol (i.e. not a space).

func WithPlugTasksListItemNotCheckedClass

func WithPlugTasksListItemNotCheckedClass[T []byte | string](class T) PlugTasksOption

WithPlugTasksListItemNotCheckedClass sets a class for a list item containing a task without status symbol: [ ].

func WithPlugTasksListItemStatusAttr

func WithPlugTasksListItemStatusAttr[T []byte | string](name T) PlugTasksOption

WithPlugTasksListItemStatusAttr sets a name for a list item attribute which will contain a task status symbol (one within [ ]). Attribute will be set to empty string if status symbol is a space.

func WithPlugTasksStatusType

func WithPlugTasksStatusType(symbol rune, statusType ast.PlugTasksStatusType) PlugTasksOption

WithPlugTasksStatusType sets a statusType for a symbol. If you need to set many symbols then using WithPlugTasksStatusTypes may be more convenient than multiple calls to WithPlugTasksStatusType.

To "disable" one of default symbols set it statusType to ast.PlugTasksStatusTypeTODO, which is used by default for all unknown symbols.

func WithPlugTasksStatusTypes

func WithPlugTasksStatusTypes(statusTypes map[rune]ast.PlugTasksStatusType) PlugTasksOption

WithPlugTasksStatusTypes sets a statusTypes for a symbols.

To "disable" one of default symbols set it statusType to ast.PlugTasksStatusTypeTODO, which is used by default for all unknown symbols.

type PlugTasksParser

type PlugTasksParser struct {
	// contains filtered or unexported fields
}

PlugTasksParser is an Obsidian plugin Tasks's task status parser.

This parser must take precedence over the parser.LinkParser.

func NewPlugTasksParser

func NewPlugTasksParser(opts ...PlugTasksOption) *PlugTasksParser

NewPlugTasksParser returns a new PlugTasksParser.

func (*PlugTasksParser) Parse

func (p *PlugTasksParser) Parse(parent gast.Node, block text.Reader, _ parser.Context) gast.Node

Parse implements parser.InlineParser.

func (*PlugTasksParser) Trigger

func (*PlugTasksParser) Trigger() []byte

Trigger implements parser.InlineParser.

type PlugTasksPropParser

type PlugTasksPropParser struct{}

PlugTasksPropParser is an Obsidian plugin Tasks's task properties parser.

Current implementation supports only properties defined in emoji style.

This parser must have lowest precedence than all other inline parsers (e.g. 1000).

func NewPlugTasksPropParser

func NewPlugTasksPropParser() PlugTasksPropParser

NewPlugTasksPropParser returns a new PlugTasksPropParser.

func (PlugTasksPropParser) Parse

func (PlugTasksPropParser) Parse(parent gast.Node, block text.Reader, _ parser.Context) gast.Node

Parse implements parser.InlineParser.

func (PlugTasksPropParser) Trigger

func (PlugTasksPropParser) Trigger() []byte

Trigger implements parser.InlineParser.

Directories

Path Synopsis
Package ast defines AST nodes that represents Obsidian's markdown elements.
Package ast defines AST nodes that represents Obsidian's markdown elements.

Jump to

Keyboard shortcuts

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