Skip to content

Commit 4ce4494

Browse files
committed
Change to expose functions
1 parent 03c4d87 commit 4ce4494

File tree

3 files changed

+140
-124
lines changed

3 files changed

+140
-124
lines changed

lib/index.js

+72-66
Original file line numberDiff line numberDiff line change
@@ -22,87 +22,93 @@ import {parseEntities} from 'parse-entities'
2222
import {stringifyEntitiesLight} from 'stringify-entities'
2323
import {visitParents} from 'unist-util-visit-parents'
2424

25-
// To do: next major: expose functions.
26-
2725
const own = {}.hasOwnProperty
2826

2927
const shortcut = /^[^\t\n\r "#'.<=>`}]+$/
3028

3129
handleDirective.peek = peekDirective
3230

3331
/**
34-
* Extension for `mdast-util-from-markdown` to enable directives.
32+
* Create an extension for `mdast-util-from-markdown` to enable directives in
33+
* markdown.
3534
*
36-
* @type {FromMarkdownExtension}
35+
* @returns {FromMarkdownExtension}
36+
* Extension for `mdast-util-from-markdown` to enable directives.
3737
*/
38-
export const directiveFromMarkdown = {
39-
canContainEols: ['textDirective'],
40-
enter: {
41-
directiveContainer: enterContainer,
42-
directiveContainerAttributes: enterAttributes,
43-
directiveContainerLabel: enterContainerLabel,
44-
45-
directiveLeaf: enterLeaf,
46-
directiveLeafAttributes: enterAttributes,
47-
48-
directiveText: enterText,
49-
directiveTextAttributes: enterAttributes
50-
},
51-
exit: {
52-
directiveContainer: exit,
53-
directiveContainerAttributeClassValue: exitAttributeClassValue,
54-
directiveContainerAttributeIdValue: exitAttributeIdValue,
55-
directiveContainerAttributeName: exitAttributeName,
56-
directiveContainerAttributeValue: exitAttributeValue,
57-
directiveContainerAttributes: exitAttributes,
58-
directiveContainerLabel: exitContainerLabel,
59-
directiveContainerName: exitName,
60-
61-
directiveLeaf: exit,
62-
directiveLeafAttributeClassValue: exitAttributeClassValue,
63-
directiveLeafAttributeIdValue: exitAttributeIdValue,
64-
directiveLeafAttributeName: exitAttributeName,
65-
directiveLeafAttributeValue: exitAttributeValue,
66-
directiveLeafAttributes: exitAttributes,
67-
directiveLeafName: exitName,
68-
69-
directiveText: exit,
70-
directiveTextAttributeClassValue: exitAttributeClassValue,
71-
directiveTextAttributeIdValue: exitAttributeIdValue,
72-
directiveTextAttributeName: exitAttributeName,
73-
directiveTextAttributeValue: exitAttributeValue,
74-
directiveTextAttributes: exitAttributes,
75-
directiveTextName: exitName
38+
export function directiveFromMarkdown() {
39+
return {
40+
canContainEols: ['textDirective'],
41+
enter: {
42+
directiveContainer: enterContainer,
43+
directiveContainerAttributes: enterAttributes,
44+
directiveContainerLabel: enterContainerLabel,
45+
46+
directiveLeaf: enterLeaf,
47+
directiveLeafAttributes: enterAttributes,
48+
49+
directiveText: enterText,
50+
directiveTextAttributes: enterAttributes
51+
},
52+
exit: {
53+
directiveContainer: exit,
54+
directiveContainerAttributeClassValue: exitAttributeClassValue,
55+
directiveContainerAttributeIdValue: exitAttributeIdValue,
56+
directiveContainerAttributeName: exitAttributeName,
57+
directiveContainerAttributeValue: exitAttributeValue,
58+
directiveContainerAttributes: exitAttributes,
59+
directiveContainerLabel: exitContainerLabel,
60+
directiveContainerName: exitName,
61+
62+
directiveLeaf: exit,
63+
directiveLeafAttributeClassValue: exitAttributeClassValue,
64+
directiveLeafAttributeIdValue: exitAttributeIdValue,
65+
directiveLeafAttributeName: exitAttributeName,
66+
directiveLeafAttributeValue: exitAttributeValue,
67+
directiveLeafAttributes: exitAttributes,
68+
directiveLeafName: exitName,
69+
70+
directiveText: exit,
71+
directiveTextAttributeClassValue: exitAttributeClassValue,
72+
directiveTextAttributeIdValue: exitAttributeIdValue,
73+
directiveTextAttributeName: exitAttributeName,
74+
directiveTextAttributeValue: exitAttributeValue,
75+
directiveTextAttributes: exitAttributes,
76+
directiveTextName: exitName
77+
}
7678
}
7779
}
7880

7981
/**
80-
* Extension for `mdast-util-to-markdown` to enable directives.
82+
* Create an extension for `mdast-util-to-markdown` to enable directives in
83+
* markdown.
8184
*
82-
* @type {ToMarkdownExtension}
85+
* @returns {ToMarkdownExtension}
86+
* Extension for `mdast-util-to-markdown` to enable directives.
8387
*/
84-
export const directiveToMarkdown = {
85-
unsafe: [
86-
{
87-
character: '\r',
88-
inConstruct: ['leafDirectiveLabel', 'containerDirectiveLabel']
89-
},
90-
{
91-
character: '\n',
92-
inConstruct: ['leafDirectiveLabel', 'containerDirectiveLabel']
93-
},
94-
{
95-
before: '[^:]',
96-
character: ':',
97-
after: '[A-Za-z]',
98-
inConstruct: ['phrasing']
99-
},
100-
{atBreak: true, character: ':', after: ':'}
101-
],
102-
handlers: {
103-
containerDirective: handleDirective,
104-
leafDirective: handleDirective,
105-
textDirective: handleDirective
88+
export function directiveToMarkdown() {
89+
return {
90+
unsafe: [
91+
{
92+
character: '\r',
93+
inConstruct: ['leafDirectiveLabel', 'containerDirectiveLabel']
94+
},
95+
{
96+
character: '\n',
97+
inConstruct: ['leafDirectiveLabel', 'containerDirectiveLabel']
98+
},
99+
{
100+
before: '[^:]',
101+
character: ':',
102+
after: '[A-Za-z]',
103+
inConstruct: ['phrasing']
104+
},
105+
{atBreak: true, character: ':', after: ':'}
106+
],
107+
handlers: {
108+
containerDirective: handleDirective,
109+
leafDirective: handleDirective,
110+
textDirective: handleDirective
111+
}
106112
}
107113
}
108114

readme.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ such).
1919
* [Install](#install)
2020
* [Use](#use)
2121
* [API](#api)
22-
* [`directiveFromMarkdown`](#directivefrommarkdown)
23-
* [`directiveToMarkdown`](#directivetomarkdown)
22+
* [`directiveFromMarkdown()`](#directivefrommarkdown)
23+
* [`directiveToMarkdown()`](#directivetomarkdown)
2424
* [`ContainerDirective`](#containerdirective)
2525
* [`Directives`](#directives)
2626
* [`LeafDirective`](#leafdirective)
@@ -123,12 +123,12 @@ const doc = await fs.readFile('example.md')
123123

124124
const tree = fromMarkdown(doc, {
125125
extensions: [directive()],
126-
mdastExtensions: [directiveFromMarkdown]
126+
mdastExtensions: [directiveFromMarkdown()]
127127
})
128128

129129
console.log(tree)
130130

131-
const out = toMarkdown(tree, {extensions: [directiveToMarkdown]})
131+
const out = toMarkdown(tree, {extensions: [directiveToMarkdown()]})
132132

133133
console.log(out)
134134
```
@@ -167,19 +167,29 @@ This package exports the identifiers
167167
[`directiveToMarkdown`][api-directive-to-markdown].
168168
There is no default export.
169169

170-
### `directiveFromMarkdown`
170+
### `directiveFromMarkdown()`
171171

172-
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown] to enable
173-
directives ([`FromMarkdownExtension`][from-markdown-extension]).
172+
Create an extension for [`mdast-util-from-markdown`][mdast-util-from-markdown]
173+
to enable directives in markdown.
174174

175-
### `directiveToMarkdown`
175+
###### Returns
176176

177-
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown] to enable
178-
directives ([`ToMarkdownExtension`][to-markdown-extension]).
177+
Extension for `mdast-util-from-markdown` to enable directives
178+
([`FromMarkdownExtension`][from-markdown-extension]).
179+
180+
### `directiveToMarkdown()`
181+
182+
Create an extension for [`mdast-util-to-markdown`][mdast-util-to-markdown]
183+
to enable directives in markdown.
179184

180185
There are no options, but passing [`options.quote`][quote] to
181186
`mdast-util-to-markdown` is honored for attributes.
182187

188+
###### Returns
189+
190+
Extension for `mdast-util-to-markdown` to enable directives
191+
([`ToMarkdownExtension`][to-markdown-extension]).
192+
183193
### `ContainerDirective`
184194

185195
Directive in flow content (such as in the root document, or block quotes),

0 commit comments

Comments
 (0)