Skip to content

Commit

Permalink
Change export map
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 27, 2023
1 parent 74159c8 commit f731487
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 44 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
coverage/
node_modules/
/lib/
/matters.js
/index.js
yarn.lock
9 changes: 5 additions & 4 deletions dev/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* @typedef {import('./matters.js').Info} Info
* @typedef {import('./matters.js').Matter} Matter
* @typedef {import('./matters.js').Options} Options
* @typedef {import('./matters.js').Preset} Preset
* @typedef {import('./lib/to-matters.js').Info} Info
* @typedef {import('./lib/to-matters.js').Matter} Matter
* @typedef {import('./lib/to-matters.js').Options} Options
* @typedef {import('./lib/to-matters.js').Preset} Preset
*/

export {frontmatter} from './lib/syntax.js'
export {frontmatterHtml} from './lib/html.js'
export {toMatters} from './lib/to-matters.js'

// Note: we don’t have an `index.d.ts` in this extension because all token
// types are dynamic in JS
10 changes: 5 additions & 5 deletions dev/lib/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @typedef {import('micromark-util-types').Handle} Handle
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
* @typedef {import('micromark-util-types').TokenType} TokenType
* @typedef {import('../matters.js').Options} Options
* @typedef {import('./to-matters.js').Options} Options
*/

import {matters} from '../matters.js'
import {toMatters} from './to-matters.js'

/**
* Create an extension for `micromark` to support frontmatter when serializing
Expand All @@ -22,15 +22,15 @@ import {matters} from '../matters.js'
* support frontmatter when serializing to HTML.
*/
export function frontmatterHtml(options) {
const listOfMatters = matters(options)
const matters = toMatters(options)
/** @type {HtmlExtension['enter']} */
const enter = {}
/** @type {HtmlExtension['exit']} */
const exit = {}
let index = -1

while (++index < listOfMatters.length) {
const type = /** @type {TokenType} */ (listOfMatters[index].type)
while (++index < matters.length) {
const type = /** @type {TokenType} */ (matters[index].type)
enter[type] = start
exit[type] = end
}
Expand Down
14 changes: 7 additions & 7 deletions dev/lib/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*
* @typedef {import('../matters.js').Info} Info
* @typedef {import('../matters.js').Matter} Matter
* @typedef {import('../matters.js').Options} Options
* @typedef {import('./to-matters.js').Info} Info
* @typedef {import('./to-matters.js').Matter} Matter
* @typedef {import('./to-matters.js').Options} Options
*/

import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
import {codes, types} from 'micromark-util-symbol'
import {matters} from '../matters.js'
import {toMatters} from './to-matters.js'

/**
* Create an extension for `micromark` to enable frontmatter syntax.
Expand All @@ -26,13 +26,13 @@ import {matters} from '../matters.js'
* enable frontmatter syntax.
*/
export function frontmatter(options) {
const listOfMatters = matters(options)
const matters = toMatters(options)
/** @type {ConstructRecord} */
const flow = {}
let index = -1

while (++index < listOfMatters.length) {
const matter = listOfMatters[index]
while (++index < matters.length) {
const matter = matters[index]
const code = fence(matter, 'open').charCodeAt(0)
const construct = createConstruct(matter)
const existing = flow[code]
Expand Down
6 changes: 3 additions & 3 deletions dev/matters.js → dev/lib/to-matters.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ const own = {}.hasOwnProperty
const markers = {yaml: '-', toml: '+'}

/**
* Simplify one or more options.
* Simplify options by normalizing them to an array of matters.
*
* @param {Options | null | undefined} [options='yaml']
* Configuration (default: `'yaml'`).
* @returns {Array<Matter>}
* List of matters.
*/
export function matters(options) {
export function toMatters(options) {
/** @type {Array<Matter>} */
const result = []
let index = -1
Expand All @@ -99,7 +99,7 @@ export function matters(options) {
* @param {Matter | Preset} option
* Configuration.
* @returns {Matter}
* Matters.
* Matter.
*/
function matter(option) {
let result = option
Expand Down
18 changes: 2 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,15 @@
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"dev/",
"lib/",
"matters.d.ts",
"matters.js",
"index.d.ts",
"index.js"
],
"exports": {
".": {
"development": "./dev/index.js",
"default": "./index.js"
},
"./matters": {
"development": "./dev/matters.js",
"default": "./matters.js"
},
"./matters.js": {
"development": "./dev/matters.js",
"default": "./matters.js"
}
"development": "./dev/index.js",
"default": "./index.js"
},
"dependencies": {
"fault": "^2.0.0",
Expand Down
20 changes: 18 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [API](#api)
* [`frontmatter(options?)`](#frontmatteroptions)
* [`frontmatterHtml(options?)`](#frontmatterhtmloptions)
* [`toMatters(options?)`](#tomattersoptions)
* [`Info`](#info)
* [`Matter`](#matter)
* [`Options`](#options)
Expand Down Expand Up @@ -107,8 +108,8 @@ console.log(output)

## API

This package exports the identifiers [`frontmatter`][api-frontmatter] and
[`frontmatterHtml`][api-frontmatter-html].
This package exports the identifiers [`frontmatter`][api-frontmatter],
[`frontmatterHtml`][api-frontmatter-html], and [`toMatters`][api-to-matters].
There is no default export.

The export map supports the [`development` condition][development].
Expand Down Expand Up @@ -148,6 +149,19 @@ Extension for `micromark` that can be passed in `htmlExtensions`, to support
frontmatter when serializing to HTML
([`HtmlExtension`][micromark-html-extension]).

### `toMatters(options?)`

Simplify options by normalizing them to an array of matters.

###### Parameters

* `options` ([`Options`][api-options], default: `['yaml']`)
— configuration

###### Returns

List of matters ([`Array<Matter>`][api-matter]).

### `Info`

Sequence (TypeScript type).
Expand Down Expand Up @@ -405,6 +419,8 @@ abide by its terms.

[api-frontmatter-html]: #frontmatterhtmloptions

[api-to-matters]: #tomattersoptions

[api-info]: #info

[api-matter]: #matter
Expand Down
10 changes: 4 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/**
* @typedef {import('../dev/matters.js').Options} Options
* @typedef {import('micromark-extension-frontmatter').Options} Options
*/

import assert from 'node:assert/strict'
import test from 'node:test'
import {micromark} from 'micromark'
// To do: use export map.
import {frontmatter, frontmatterHtml} from '../dev/index.js'
import {frontmatter, frontmatterHtml} from 'micromark-extension-frontmatter'

test('frontmatter', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
// To do: use export map.
Object.keys(await import('../dev/index.js')).sort(),
['frontmatter', 'frontmatterHtml']
Object.keys(await import('micromark-extension-frontmatter')).sort(),
['frontmatter', 'frontmatterHtml', 'toMatters']
)
})

Expand Down

0 comments on commit f731487

Please sign in to comment.