Skip to content

Commit

Permalink
Refactor to use @imports
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 5, 2024
1 parent a1974c0 commit 727a56e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules/
/lib/
/index.js
yarn.lock
!dev/index.d.ts
100 changes: 100 additions & 0 deletions dev/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
export {frontmatterHtml} from './lib/html.js'
export {frontmatter} from './lib/syntax.js'
export {toMatters} from './lib/to-matters.js'

/**
* Sequence.
*
* Depending on how this structure is used, it reflects a marker or a fence.
*/
export interface Info {
/**
* Closing.
*/
close: string
/**
* Opening.
*/
open: string
}

/**
* Fence configuration.
*/
interface FenceFields {
/**
* Complete fences.
*
* This can be used when fences contain different characters or lengths
* other than 3.
* Pass `open` and `close` to interface to specify different characters for opening and
* closing fences.
*/
fence: Info | string
/**
* If `fence` is set, `marker` must not be set.
*/
marker?: never
}

/**
* Marker configuration.
*/
interface MarkerFields {
/**
* Character repeated 3 times, used as complete fences.
*
* For example the character `'-'` will result in `'---'` being used as the
* fence
* Pass `open` and `close` to specify different characters for opening and
* closing fences.
*/
marker: Info | string
/**
* If `marker` is set, `fence` must not be set.
*/
fence?: never
}

/**
* Fields describing a kind of matter.
*/
interface MatterFields {
/**
* Node type to tokenize as.
*/
type: string
/**
* Whether matter can be found anywhere in the document, normally, only matter
* at the start of the document is recognized.
*
* > 👉 **Note**: using this is a terrible idea.
* > It’s called frontmatter, not matter-in-the-middle or so.
* > This makes your markdown less portable.
*/
anywhere?: boolean | null | undefined
}

/**
* Fields describing a kind of matter.
*
* > 👉 **Note**: using `anywhere` is a terrible idea.
* > It’s called frontmatter, not matter-in-the-middle or so.
* > This makes your markdown less portable.
*
* > 👉 **Note**: `marker` and `fence` are mutually exclusive.
* > If `marker` is set, `fence` must not be set, and vice versa.
*/
export type Matter =
| (MatterFields & FenceFields)
| (MatterFields & MarkerFields)

/**
* Configuration.
*/
export type Options = Matter | Preset | Array<Matter | Preset>

/**
* Known name of a frontmatter style.
*/
export type Preset = 'toml' | 'yaml'
11 changes: 1 addition & 10 deletions dev/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
/**
* @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
*/

// Note: types exposed from `index.d.ts`.
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
7 changes: 2 additions & 5 deletions dev/lib/html.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/**
* @typedef {import('micromark-util-types').CompileContext} CompileContext
* @typedef {import('micromark-util-types').Handle} Handle
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
* @typedef {import('micromark-util-types').TokenType} TokenType
* @typedef {import('./to-matters.js').Options} Options
* @import {Options} from 'micromark-extension-frontmatter'
* @import {CompileContext, Handle, HtmlExtension, TokenType} from 'micromark-util-types'
*/

import {toMatters} from './to-matters.js'
Expand Down
13 changes: 2 additions & 11 deletions dev/lib/syntax.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').ConstructRecord} ConstructRecord
* @typedef {import('micromark-util-types').Extension} Extension
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*
* @typedef {import('./to-matters.js').Info} Info
* @typedef {import('./to-matters.js').Matter} Matter
* @typedef {import('./to-matters.js').Options} Options
* @import {Info, Matter, Options} from 'micromark-extension-frontmatter'
* @import {Construct, ConstructRecord, Extension, State, TokenType, TokenizeContext, Tokenizer} from 'micromark-util-types'
*/

import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
Expand Down
61 changes: 1 addition & 60 deletions dev/lib/to-matters.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,5 @@
/**
* @typedef {'toml' | 'yaml'} Preset
* Known name of a frontmatter style.
*
* @typedef Info
* Sequence.
*
* Depending on how this structure is used, it reflects a marker or a fence.
* @property {string} close
* Closing.
* @property {string} open
* Opening.
*
* @typedef MatterProps
* Fields describing a kind of matter.
* @property {string} type
* Node type to tokenize as.
* @property {boolean | null | undefined} [anywhere=false]
* Whether matter can be found anywhere in the document, normally, only matter
* at the start of the document is recognized.
*
* > 👉 **Note**: using this is a terrible idea.
* > It’s called frontmatter, not matter-in-the-middle or so.
* > This makes your markdown less portable.
*
* @typedef MarkerProps
* Marker configuration.
* @property {Info | string} marker
* Character repeated 3 times, used as complete fences.
*
* For example the character `'-'` will result in `'---'` being used as the
* fence
* Pass `open` and `close` to specify different characters for opening and
* closing fences.
* @property {never} [fence]
* If `marker` is set, `fence` must not be set.
*
* @typedef FenceProps
* Fence configuration.
* @property {Info | string} fence
* Complete fences.
*
* This can be used when fences contain different characters or lengths
* other than 3.
* Pass `open` and `close` to interface to specify different characters for opening and
* closing fences.
* @property {never} [marker]
* If `fence` is set, `marker` must not be set.
*
* @typedef {(MatterProps & FenceProps) | (MatterProps & MarkerProps)} Matter
* Fields describing a kind of matter.
*
* > 👉 **Note**: using `anywhere` is a terrible idea.
* > It’s called frontmatter, not matter-in-the-middle or so.
* > This makes your markdown less portable.
*
* > 👉 **Note**: `marker` and `fence` are mutually exclusive.
* > If `marker` is set, `fence` must not be set, and vice versa.
*
* @typedef {Matter | Preset | Array<Matter | Preset>} Options
* Configuration.
* @import {Matter, Options, Preset} from 'micromark-extension-frontmatter'
*/

import {fault} from 'fault'
Expand Down
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@
"strict": true
},
"xo": {
"overrides": [
{
"files": [
"**/*.d.ts"
],
"rules": {
"@typescript-eslint/array-type": [
"error",
{
"default": "generic"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true
}
],
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
]
}
}
],
"prettier": true,
"rules": {
"unicorn/no-this-assignment": "off",
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('micromark-extension-frontmatter').Options} Options
* @import {Options} from 'micromark-extension-frontmatter'
*/

import assert from 'node:assert/strict'
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"target": "es2022"
},
"exclude": ["coverage/", "lib/", "node_modules/", "index.js"],
"include": ["**/*.js"]
"include": ["**/*.js", "dev/index.d.ts"]
}

0 comments on commit 727a56e

Please sign in to comment.