0.2.0 • Published 1 month ago
@stereobooster/mdast-util-wiki-link v0.2.0
@stereobooster/mdast-util-wiki-link
Fork of mdast-util-wiki-link to simplify options.
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 linkdata.permalink
: The permalink for this page. This permalink is computed fromnode.value
usingoptions.linkResolver
, which can be passed in when initializing the plugin.data.h...
: provide compatibility withrehype
. Computed fromdata
usingoptions.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>