@stereobooster/mdast-util-wiki-link
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@stereobooster/mdast-util-wiki-link

Fork of mdast-util-wiki-link to simplify options.

npm version Build Status

Extension for mdast-util-from-markdown and mdast-util-to-markdown to support [[Wiki Links]].

  • Parse wiki-style links and render them as anchors
  • Parse aliased wiki links i.e [[Real Page:Page Alias]]

Using remark? You might want to use @stereobooster/remark-wiki-link instead of using this package directly.

Usage

Markdown to AST

import fromMarkdown from 'mdast-util-from-markdown'
import { syntax } from 'micromark-extension-wiki-link'
import * as wikiLink from 'mdast-util-wiki-link'

let ast = fromMarkdown('[[Test Page]]', {
  extensions: [syntax()],
  mdastExtensions: [wikiLink.fromMarkdown()]
})

The AST node will look like this:

{
    value: 'Test Page',
    data: {
        alias: 'Test Page',
        permalink: 'Test Page',
        hName: 'a',
        hProperties: {
            href: 'Test Page'
        },
        hChildren: [{
            type: 'text',
            value: 'Test Page'
        }]
    }
}
  • data.alias: The display name for this link
  • data.permalink: The permalink for this page. This permalink is computed from node.value using options.linkResolver, which can be passed in when initializing the plugin.
  • data.h...: provide compatibility with rehype. Computed from data using options.linkTemplate

AST to Markdown

Taking the ast from the prior example, let's go back to markdown:

import { fromMarkdown } from 'mdast-util-from-markdown'
import * as wikiLink from 'mdast-util-wiki-link'

let markdownString = toMarkdown(ast, { extensions: [wikiLink.toMarkdown()] }).trim()
console.log(markdownString)
// [[Wiki Link]]

Configuration options

Both fromMarkdown and toMarkdown accept configuration as an object.

For example, one may configure fromMarkdown like so:

let ast = fromMarkdown('[[Test Page]]', {
  extensions: [syntax()],
  mdastExtensions: [wikiLink.fromMarkdown({ linkResolver: (x) => x })] // <--
})

fromMarkdown

  • options.linkResolver (pageName: String) -> String: A function that maps a page name to a permalink.
  • options.linkTemplate (opts: { permalink: string, alias: string }) -> hast: A function that creates hast representation of wiki link. Default value is:
function defaultLinkTemplate ({ permalink, alias }: LinkTemplateProps) {
  return {
    hName: 'a',
    hProperties: { href: permalink },
    hChildren: [{ type: 'text', value: alias }]
  }
}

toMarkdown

  • options.aliasDivider [String]: a string to be used as the divider for aliases. See the section below on Aliasing pages. Defaults to ":".

Aliasing pages

Aliased pages are supported with the following markdown syntax:

[[Real Page:Page Alias]]

And will produce this HTML when rendered:

<a href="Real Page">Page Alias</a>

Package Sidebar

Install

npm i @stereobooster/mdast-util-wiki-link

Weekly Downloads

3

Version

0.2.0

License

MIT

Unpacked Size

26 kB

Total Files

22

Last publish

Collaborators

  • stereobooster