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 fdc0fa5 commit c33a640
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 105 deletions.
100 changes: 88 additions & 12 deletions dev/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,95 @@
import type {Attribute, Directive} from './lib/html.js'
import type {CompileContext} from 'micromark-util-types'

export {directive} from './lib/syntax.js'
export {
directiveHtml,
type Directive,
type Handle,
type HtmlOptions
} from './lib/html.js'
export {directiveHtml} from './lib/html.js'

/**
* Internal tuple representing an attribute.
*/
type AttributeTuple = [key: string, value: string]

/**
* Directive attribute.
*/
interface Attributes {
/**
* Key to value.
*/
[key: string]: string
}

/**
* Structure representing a directive.
*/
export interface Directive {
/**
* Private :)
*/
_fenceCount?: number | undefined
/**
* Object w/ HTML attributes.
*/
attributes?: Attributes | undefined
/**
* Compiled HTML content inside container directive.
*/
content?: string | undefined
/**
* Compiled HTML content that was in `[brackets]`.
*/
label?: string | undefined
/**
* Name of directive.
*/
name: string
/**
* Kind.
*/
type: 'containerDirective' | 'leafDirective' | 'textDirective'
}

/**
* Handle a directive.
*
* @param this
* Current context.
* @param directive
* Directive.
* @returns
* Signal whether the directive was handled.
*
* Yield `false` to let the fallback (a special handle for `'*'`) handle it.
*/
export type Handle = (
this: CompileContext,
directive: Directive
) => boolean | undefined

/**
* Configuration.
*
* > 👉 **Note**: the special field `'*'` can be used to specify a fallback
* > handle to handle all otherwise unhandled directives.
*/
export interface HtmlOptions {
[name: string]: Handle
}

/**
* Augment types.
*/
declare module 'micromark-util-types' {
/**
* Compile data.
*/
interface CompileData {
directiveAttributes?: Array<AttributeTuple>
directiveStack?: Array<Directive>
}

/**
* Token types.
*/
interface TokenTypeMap {
directiveContainer: 'directiveContainer'
directiveContainerAttributes: 'directiveContainerAttributes'
Expand Down Expand Up @@ -72,9 +153,4 @@ declare module 'micromark-util-types' {
directiveTextMarker: 'directiveTextMarker'
directiveTextName: 'directiveTextName'
}

interface CompileData {
directiveAttributes?: Attribute[]
directiveStack?: Directive[]
}
}
6 changes: 1 addition & 5 deletions dev/lib/directive-container.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Token} Token
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @import {Construct, State, Token, TokenizeContext, Tokenizer} from 'micromark-util-types'
*/

import {ok as assert} from 'devlop'
Expand Down
5 changes: 1 addition & 4 deletions dev/lib/directive-leaf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @import {Construct, State, TokenizeContext, Tokenizer} from 'micromark-util-types'
*/

import {ok as assert} from 'devlop'
Expand Down
6 changes: 1 addition & 5 deletions dev/lib/directive-text.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').Previous} Previous
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @import {Construct, Previous, State, TokenizeContext, Tokenizer} from 'micromark-util-types'
*/

import {ok as assert} from 'devlop'
Expand Down
5 changes: 1 addition & 4 deletions dev/lib/factory-attributes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @typedef {import('micromark-util-types').Code} Code
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
* @import {Code, Effects, State, TokenType} from 'micromark-util-types'
*/

import {ok as assert} from 'devlop'
Expand Down
5 changes: 1 addition & 4 deletions dev/lib/factory-label.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Token} Token
* @typedef {import('micromark-util-types').TokenType} TokenType
* @import {Code, Effects, State, Token, TokenType} from 'micromark-util-types'
*/

import {ok as assert} from 'devlop'
Expand Down
5 changes: 1 addition & 4 deletions dev/lib/factory-name.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').TokenType} TokenType
* @import {Code, Effects, State, TokenizeContext, TokenType} from 'micromark-util-types'
*/

import {asciiAlpha, asciiAlphanumeric} from 'micromark-util-character'
Expand Down
75 changes: 16 additions & 59 deletions dev/lib/html.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
/**
* @typedef {import('micromark-util-types').CompileContext} CompileContext
* @typedef {import('micromark-util-types').Handle} _Handle
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
*/

/**
* @typedef {[string, string]} Attribute
* Internal tuple representing an attribute.
*/

/**
* @typedef {Record<string, Handle>} HtmlOptions
* Configuration.
*
* > 👉 **Note**: the special field `'*'` can be used to specify a fallback
* > handle to handle all otherwise unhandled directives.
*
* @callback Handle
* Handle a directive.
* @param {CompileContext} this
* Current context.
* @param {Directive} directive
* Directive.
* @returns {boolean | undefined}
* Signal whether the directive was handled.
*
* Yield `false` to let the fallback (a special handle for `'*'`) handle it.
*
* @typedef Directive
* Structure representing a directive.
* @property {DirectiveType} type
* Kind.
* @property {string} name
* Name of directive.
* @property {string | undefined} [label]
* Compiled HTML content that was in `[brackets]`.
* @property {Record<string, string> | undefined} [attributes]
* Object w/ HTML attributes.
* @property {string | undefined} [content]
* Compiled HTML content inside container directive.
* @property {number | undefined} [_fenceCount]
* Private :)
*
* @typedef {'containerDirective' | 'leafDirective' | 'textDirective'} DirectiveType
* Kind.
* @import {Directive, HtmlOptions} from 'micromark-extension-directive'
* @import {CompileContext, Handle as MicromarkHandle, HtmlExtension} from 'micromark-util-types'
*/

import {ok as assert} from 'devlop'
Expand Down Expand Up @@ -120,7 +77,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @param {DirectiveType} type
* @param {Directive['type']} type
*/
function enter(type) {
let stack = this.getData('directiveStack')
Expand All @@ -130,7 +87,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitName(token) {
const stack = this.getData('directiveStack')
Expand All @@ -140,15 +97,15 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function enterLabel() {
this.buffer()
}

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitLabel() {
const data = this.resume()
Expand All @@ -159,7 +116,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function enterAttributes() {
this.buffer()
Expand All @@ -168,7 +125,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitAttributeIdValue(token) {
const attributes = this.getData('directiveAttributes')
Expand All @@ -183,7 +140,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitAttributeClassValue(token) {
const attributes = this.getData('directiveAttributes')
Expand All @@ -199,7 +156,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitAttributeName(token) {
// Attribute names in CommonMark are significantly limited, so character
Expand All @@ -212,7 +169,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitAttributeValue(token) {
const attributes = this.getData('directiveAttributes')
Expand All @@ -225,14 +182,14 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitAttributes() {
const stack = this.getData('directiveStack')
assert(stack, 'expected directive stack')
const attributes = this.getData('directiveAttributes')
assert(attributes, 'expected attributes')
/** @type {Directive['attributes']} */
/** @type {Record<string, string>} */
const cleaned = {}
let index = -1

Expand All @@ -253,7 +210,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitContainerContent() {
const data = this.resume()
Expand All @@ -264,7 +221,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exitContainerFence() {
const stack = this.getData('directiveStack')
Expand All @@ -277,7 +234,7 @@ export function directiveHtml(options) {

/**
* @this {CompileContext}
* @type {_Handle}
* @type {MicromarkHandle}
*/
function exit() {
const stack = this.getData('directiveStack')
Expand Down
2 changes: 1 addition & 1 deletion dev/lib/syntax.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('micromark-util-types').Extension} Extension
* @import {Extension} from 'micromark-util-types'
*/

import {codes} from 'micromark-util-symbol'
Expand Down
23 changes: 21 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,29 @@
"overrides": [
{
"files": [
"**/*.ts"
"**/*.d.ts"
],
"rules": {
"@typescript-eslint/consistent-type-definitions": "off"
"@typescript-eslint/array-type": [
"error",
{
"default": "generic"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true
}
],
"@typescript-eslint/consistent-indexed-object-style": [
"error",
"index-signature"
],
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
]
}
}
],
Expand Down
Loading

0 comments on commit c33a640

Please sign in to comment.