Skip to content

Commit

Permalink
Remove leaf and text directives
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding committed Mar 1, 2024
1 parent 5dd6e97 commit 036731c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 803 deletions.
116 changes: 1 addition & 115 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,62 +48,12 @@ export interface ContainerDirective extends Parent, DirectiveFields {
*/
export interface ContainerDirectiveData extends Data {}

/**
* Markdown directive (leaf form).
*/
export interface LeafDirective extends Parent, DirectiveFields {
/**
* Node type of leaf directive.
*/
type: 'leafDirective'

/**
* Children of leaf directive.
*/
children: PhrasingContent[]

/**
* Data associated with the mdast leaf directive.
*/
data?: LeafDirectiveData | undefined
}

/**
* Info associated with mdast leaf directive nodes by the ecosystem.
*/
export interface LeafDirectiveData extends Data {}

/**
* Markdown directive (text form).
*/
export interface TextDirective extends Parent, DirectiveFields {
/**
* Node type of text directive.
*/
type: 'textDirective'

/**
* Children of text directive.
*/
children: PhrasingContent[]

/**
* Data associated with the text leaf directive.
*/
data?: TextDirectiveData | undefined
}

/**
* Info associated with mdast text directive nodes by the ecosystem.
*/
export interface TextDirectiveData extends Data {}

/**
* Union of registered mdast directive nodes.
*
* It is not possible to register custom mdast directive node types.
*/
export type Directives = ContainerDirective | LeafDirective | TextDirective
export type Directives = ContainerDirective

// Add custom data tracked to turn markdown into a tree.
declare module 'mdast-util-from-markdown' {
Expand Down Expand Up @@ -140,46 +90,6 @@ declare module 'mdast-util-to-markdown' {
* ```
*/
containerDirectiveLabel: 'containerDirectiveLabel'

/**
* Whole leaf directive.
*
* ```markdown
* > | ::a
* ^^^
* ```
*/
leafDirective: 'leafDirective'

/**
* Label of a leaf directive.
*
* ```markdown
* > | ::a[b]
* ^^^
* ```
*/
leafDirectiveLabel: 'leafDirectiveLabel'

/**
* Whole text directive.
*
* ```markdown
* > | :a
* ^^
* ```
*/
textDirective: 'textDirective'

/**
* Label of a text directive.
*
* ```markdown
* > | :a[b]
* ^^^
* ```
*/
textDirectiveLabel: 'textDirectiveLabel'
}
}

Expand All @@ -191,12 +101,6 @@ declare module 'mdast' {
* quotes), which contains further flow content.
*/
containerDirective: ContainerDirective

/**
* Directive in flow content (such as in the root document, or block
* quotes), which contains nothing.
*/
leafDirective: LeafDirective
}

interface ParagraphData {
Expand All @@ -214,29 +118,11 @@ declare module 'mdast' {
directiveLabel?: boolean | null | undefined
}

interface PhrasingContentMap {
/**
* Directive in phrasing content (such as in paragraphs, headings).
*/
textDirective: TextDirective
}

interface RootContentMap {
/**
* Directive in flow content (such as in the root document, or block
* quotes), which contains further flow content.
*/
containerDirective: ContainerDirective

/**
* Directive in flow content (such as in the root document, or block
* quotes), which contains nothing.
*/
leafDirective: LeafDirective

/**
* Directive in phrasing content (such as in paragraphs, headings).
*/
textDirective: TextDirective
}
}
82 changes: 10 additions & 72 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* @typedef {import('mdast-util-to-markdown').State} State
*
* @typedef {import('../index.js').Directives} Directives
* @typedef {import('../index.js').LeafDirective} LeafDirective
* @typedef {import('../index.js').TextDirective} TextDirective
*/

import {ok as assert} from 'devlop'
Expand All @@ -41,13 +39,7 @@ export function directiveFromMarkdown() {
enter: {
directiveContainer: enterContainer,
directiveContainerAttributes: enterAttributes,
directiveContainerLabel: enterContainerLabel,

directiveLeaf: enterLeaf,
directiveLeafAttributes: enterAttributes,

directiveText: enterText,
directiveTextAttributes: enterAttributes
directiveContainerLabel: enterContainerLabel
},
exit: {
directiveContainer: exit,
Expand All @@ -57,23 +49,7 @@ export function directiveFromMarkdown() {
directiveContainerAttributeValue: exitAttributeValue,
directiveContainerAttributes: exitAttributes,
directiveContainerLabel: exitContainerLabel,
directiveContainerName: exitName,

directiveLeaf: exit,
directiveLeafAttributeClassValue: exitAttributeClassValue,
directiveLeafAttributeIdValue: exitAttributeIdValue,
directiveLeafAttributeName: exitAttributeName,
directiveLeafAttributeValue: exitAttributeValue,
directiveLeafAttributes: exitAttributes,
directiveLeafName: exitName,

directiveText: exit,
directiveTextAttributeClassValue: exitAttributeClassValue,
directiveTextAttributeIdValue: exitAttributeIdValue,
directiveTextAttributeName: exitAttributeName,
directiveTextAttributeValue: exitAttributeValue,
directiveTextAttributes: exitAttributes,
directiveTextName: exitName
directiveContainerName: exitName
}
}
}
Expand All @@ -90,11 +66,11 @@ export function directiveToMarkdown() {
unsafe: [
{
character: '\r',
inConstruct: ['leafDirectiveLabel', 'containerDirectiveLabel']
inConstruct: ['containerDirectiveLabel']
},
{
character: '\n',
inConstruct: ['leafDirectiveLabel', 'containerDirectiveLabel']
inConstruct: ['containerDirectiveLabel']
},
{
before: '[^:]',
Expand All @@ -105,9 +81,7 @@ export function directiveToMarkdown() {
{atBreak: true, character: ':', after: ':'}
],
handlers: {
containerDirective: handleDirective,
leafDirective: handleDirective,
textDirective: handleDirective
containerDirective: handleDirective
}
}
}
Expand All @@ -120,22 +94,6 @@ function enterContainer(token) {
enter.call(this, 'containerDirective', token)
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function enterLeaf(token) {
enter.call(this, 'leafDirective', token)
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function enterText(token) {
enter.call(this, 'textDirective', token)
}

/**
* @this {CompileContext}
* @param {Directives['type']} type
Expand All @@ -151,11 +109,7 @@ function enter(type, token) {
*/
function exitName(token) {
const node = this.stack[this.stack.length - 1]
assert(
node.type === 'containerDirective' ||
node.type === 'leafDirective' ||
node.type === 'textDirective'
)
assert(node.type === 'containerDirective')
node.name = this.sliceSerialize(token)
}

Expand Down Expand Up @@ -266,11 +220,7 @@ function exitAttributes() {
this.data.directiveAttributes = undefined
this.resume() // Drop EOLs
const node = this.stack[this.stack.length - 1]
assert(
node.type === 'containerDirective' ||
node.type === 'leafDirective' ||
node.type === 'textDirective'
)
assert(node.type === 'containerDirective')
node.attributes = cleaned
}

Expand All @@ -291,15 +241,9 @@ function handleDirective(node, _, state, info) {
const sequence = fence(node)
const exit = state.enter(node.type)
let value = tracker.move(sequence + (node.name || ''))
/** @type {LeafDirective | Paragraph | TextDirective | undefined} */
let label

if (node.type === 'containerDirective') {
const head = (node.children || [])[0]
label = inlineDirectiveLabel(head) ? head : undefined
} else {
label = node
}
const head = (node.children || [])[0]
const label = inlineDirectiveLabel(head) ? head : undefined

if (label && label.children && label.children.length > 0) {
const exit = state.enter('label')
Expand All @@ -308,10 +252,6 @@ function handleDirective(node, _, state, info) {
const subexit = state.enter(labelType)
value += tracker.move('[')
value += tracker.move(
// @ts-expect-error: `containerPhrasing` is typed correctly, but TS
// generates *hardcoded* types, which means that our dynamically added
// directives are not present.
// At some point, TS should fix that, and `from-markdown` should be fine.
state.containerPhrasing(label, {
...tracker.current(),
before: value,
Expand Down Expand Up @@ -357,7 +297,7 @@ function peekDirective() {
*/
function attributes(node, state) {
const quote = state.options.quote || '"'
const subset = node.type === 'textDirective' ? [quote] : [quote, '\n', '\r']
const subset = [quote, '\n', '\r']
const attrs = node.attributes || {}
/** @type {Array<string>} */
const values = []
Expand Down Expand Up @@ -467,8 +407,6 @@ function fence(node) {
}
})
size += 3
} else if (node.type === 'leafDirective') {
size = 2
} else {
size = 1
}
Expand Down
Loading

0 comments on commit 036731c

Please sign in to comment.