New source found from dndbeyond.com
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {Blockquote} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `blockquote` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Blockquote} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function blockquote(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'blockquote',
|
||||
properties: {},
|
||||
children: state.wrap(state.all(node), true)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @import {Element, Text} from 'hast'
|
||||
* @import {Break} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `break` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Break} node
|
||||
* mdast node.
|
||||
* @returns {Array<Element | Text>}
|
||||
* hast element content.
|
||||
*/
|
||||
export function hardBreak(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {type: 'element', tagName: 'br', properties: {}, children: []}
|
||||
state.patch(node, result)
|
||||
return [state.applyData(node, result), {type: 'text', value: '\n'}]
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @import {Element, Properties} from 'hast'
|
||||
* @import {Code} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `code` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Code} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function code(state, node) {
|
||||
const value = node.value ? node.value + '\n' : ''
|
||||
/** @type {Properties} */
|
||||
const properties = {}
|
||||
// Someone can write `js python	ruby`.
|
||||
const language = node.lang ? node.lang.split(/\s+/) : []
|
||||
|
||||
// GH/CM still drop the non-first languages.
|
||||
if (language.length > 0) {
|
||||
properties.className = ['language-' + language[0]]
|
||||
}
|
||||
|
||||
// Create `<code>`.
|
||||
/** @type {Element} */
|
||||
let result = {
|
||||
type: 'element',
|
||||
tagName: 'code',
|
||||
properties,
|
||||
children: [{type: 'text', value}]
|
||||
}
|
||||
|
||||
if (node.meta) {
|
||||
result.data = {meta: node.meta}
|
||||
}
|
||||
|
||||
state.patch(node, result)
|
||||
result = state.applyData(node, result)
|
||||
|
||||
// Create `<pre>`.
|
||||
result = {type: 'element', tagName: 'pre', properties: {}, children: [result]}
|
||||
state.patch(node, result)
|
||||
return result
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {Delete} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `delete` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Delete} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function strikethrough(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'del',
|
||||
properties: {},
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {Emphasis} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `emphasis` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Emphasis} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function emphasis(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'em',
|
||||
properties: {},
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {FootnoteReference} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
import {normalizeUri} from 'micromark-util-sanitize-uri'
|
||||
|
||||
/**
|
||||
* Turn an mdast `footnoteReference` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {FootnoteReference} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function footnoteReference(state, node) {
|
||||
const clobberPrefix =
|
||||
typeof state.options.clobberPrefix === 'string'
|
||||
? state.options.clobberPrefix
|
||||
: 'user-content-'
|
||||
const id = String(node.identifier).toUpperCase()
|
||||
const safeId = normalizeUri(id.toLowerCase())
|
||||
const index = state.footnoteOrder.indexOf(id)
|
||||
/** @type {number} */
|
||||
let counter
|
||||
|
||||
let reuseCounter = state.footnoteCounts.get(id)
|
||||
|
||||
if (reuseCounter === undefined) {
|
||||
reuseCounter = 0
|
||||
state.footnoteOrder.push(id)
|
||||
counter = state.footnoteOrder.length
|
||||
} else {
|
||||
counter = index + 1
|
||||
}
|
||||
|
||||
reuseCounter += 1
|
||||
state.footnoteCounts.set(id, reuseCounter)
|
||||
|
||||
/** @type {Element} */
|
||||
const link = {
|
||||
type: 'element',
|
||||
tagName: 'a',
|
||||
properties: {
|
||||
href: '#' + clobberPrefix + 'fn-' + safeId,
|
||||
id:
|
||||
clobberPrefix +
|
||||
'fnref-' +
|
||||
safeId +
|
||||
(reuseCounter > 1 ? '-' + reuseCounter : ''),
|
||||
dataFootnoteRef: true,
|
||||
ariaDescribedBy: ['footnote-label']
|
||||
},
|
||||
children: [{type: 'text', value: String(counter)}]
|
||||
}
|
||||
state.patch(node, link)
|
||||
|
||||
/** @type {Element} */
|
||||
const sup = {
|
||||
type: 'element',
|
||||
tagName: 'sup',
|
||||
properties: {},
|
||||
children: [link]
|
||||
}
|
||||
state.patch(node, sup)
|
||||
return state.applyData(node, sup)
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {Heading} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `heading` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Heading} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function heading(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'h' + node.depth,
|
||||
properties: {},
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {Html} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
* @import {Raw} from '../../index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `html` node into hast (`raw` node in dangerous mode, otherwise
|
||||
* nothing).
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Html} node
|
||||
* mdast node.
|
||||
* @returns {Element | Raw | undefined}
|
||||
* hast node.
|
||||
*/
|
||||
export function html(state, node) {
|
||||
if (state.options.allowDangerousHtml) {
|
||||
/** @type {Raw} */
|
||||
const result = {type: 'raw', value: node.value}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @import {ElementContent, Element, Properties} from 'hast'
|
||||
* @import {ImageReference} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
import {normalizeUri} from 'micromark-util-sanitize-uri'
|
||||
import {revert} from '../revert.js'
|
||||
|
||||
/**
|
||||
* Turn an mdast `imageReference` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {ImageReference} node
|
||||
* mdast node.
|
||||
* @returns {Array<ElementContent> | ElementContent}
|
||||
* hast node.
|
||||
*/
|
||||
export function imageReference(state, node) {
|
||||
const id = String(node.identifier).toUpperCase()
|
||||
const definition = state.definitionById.get(id)
|
||||
|
||||
if (!definition) {
|
||||
return revert(state, node)
|
||||
}
|
||||
|
||||
/** @type {Properties} */
|
||||
const properties = {src: normalizeUri(definition.url || ''), alt: node.alt}
|
||||
|
||||
if (definition.title !== null && definition.title !== undefined) {
|
||||
properties.title = definition.title
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {type: 'element', tagName: 'img', properties, children: []}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @import {Element, Properties} from 'hast'
|
||||
* @import {Image} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
import {normalizeUri} from 'micromark-util-sanitize-uri'
|
||||
|
||||
/**
|
||||
* Turn an mdast `image` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Image} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function image(state, node) {
|
||||
/** @type {Properties} */
|
||||
const properties = {src: normalizeUri(node.url)}
|
||||
|
||||
if (node.alt !== null && node.alt !== undefined) {
|
||||
properties.alt = node.alt
|
||||
}
|
||||
|
||||
if (node.title !== null && node.title !== undefined) {
|
||||
properties.title = node.title
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {type: 'element', tagName: 'img', properties, children: []}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* @import {Handlers} from '../state.js'
|
||||
*/
|
||||
|
||||
import {blockquote} from './blockquote.js'
|
||||
import {hardBreak} from './break.js'
|
||||
import {code} from './code.js'
|
||||
import {strikethrough} from './delete.js'
|
||||
import {emphasis} from './emphasis.js'
|
||||
import {footnoteReference} from './footnote-reference.js'
|
||||
import {heading} from './heading.js'
|
||||
import {html} from './html.js'
|
||||
import {imageReference} from './image-reference.js'
|
||||
import {image} from './image.js'
|
||||
import {inlineCode} from './inline-code.js'
|
||||
import {linkReference} from './link-reference.js'
|
||||
import {link} from './link.js'
|
||||
import {listItem} from './list-item.js'
|
||||
import {list} from './list.js'
|
||||
import {paragraph} from './paragraph.js'
|
||||
import {root} from './root.js'
|
||||
import {strong} from './strong.js'
|
||||
import {table} from './table.js'
|
||||
import {tableRow} from './table-row.js'
|
||||
import {tableCell} from './table-cell.js'
|
||||
import {text} from './text.js'
|
||||
import {thematicBreak} from './thematic-break.js'
|
||||
|
||||
/**
|
||||
* Default handlers for nodes.
|
||||
*
|
||||
* @satisfies {Handlers}
|
||||
*/
|
||||
export const handlers = {
|
||||
blockquote,
|
||||
break: hardBreak,
|
||||
code,
|
||||
delete: strikethrough,
|
||||
emphasis,
|
||||
footnoteReference,
|
||||
heading,
|
||||
html,
|
||||
imageReference,
|
||||
image,
|
||||
inlineCode,
|
||||
linkReference,
|
||||
link,
|
||||
listItem,
|
||||
list,
|
||||
paragraph,
|
||||
// @ts-expect-error: root is different, but hard to type.
|
||||
root,
|
||||
strong,
|
||||
table,
|
||||
tableCell,
|
||||
tableRow,
|
||||
text,
|
||||
thematicBreak,
|
||||
toml: ignore,
|
||||
yaml: ignore,
|
||||
definition: ignore,
|
||||
footnoteDefinition: ignore
|
||||
}
|
||||
|
||||
// Return nothing for nodes that are ignored.
|
||||
function ignore() {
|
||||
return undefined
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @import {Element, Text} from 'hast'
|
||||
* @import {InlineCode} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `inlineCode` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {InlineCode} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function inlineCode(state, node) {
|
||||
/** @type {Text} */
|
||||
const text = {type: 'text', value: node.value.replace(/\r?\n|\r/g, ' ')}
|
||||
state.patch(node, text)
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'code',
|
||||
properties: {},
|
||||
children: [text]
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @import {ElementContent, Element, Properties} from 'hast'
|
||||
* @import {LinkReference} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
import {normalizeUri} from 'micromark-util-sanitize-uri'
|
||||
import {revert} from '../revert.js'
|
||||
|
||||
/**
|
||||
* Turn an mdast `linkReference` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {LinkReference} node
|
||||
* mdast node.
|
||||
* @returns {Array<ElementContent> | ElementContent}
|
||||
* hast node.
|
||||
*/
|
||||
export function linkReference(state, node) {
|
||||
const id = String(node.identifier).toUpperCase()
|
||||
const definition = state.definitionById.get(id)
|
||||
|
||||
if (!definition) {
|
||||
return revert(state, node)
|
||||
}
|
||||
|
||||
/** @type {Properties} */
|
||||
const properties = {href: normalizeUri(definition.url || '')}
|
||||
|
||||
if (definition.title !== null && definition.title !== undefined) {
|
||||
properties.title = definition.title
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'a',
|
||||
properties,
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @import {Element, Properties} from 'hast'
|
||||
* @import {Link} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
import {normalizeUri} from 'micromark-util-sanitize-uri'
|
||||
|
||||
/**
|
||||
* Turn an mdast `link` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Link} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function link(state, node) {
|
||||
/** @type {Properties} */
|
||||
const properties = {href: normalizeUri(node.url)}
|
||||
|
||||
if (node.title !== null && node.title !== undefined) {
|
||||
properties.title = node.title
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'a',
|
||||
properties,
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @import {ElementContent, Element, Properties} from 'hast'
|
||||
* @import {ListItem, Parents} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `listItem` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {ListItem} node
|
||||
* mdast node.
|
||||
* @param {Parents | undefined} parent
|
||||
* Parent of `node`.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function listItem(state, node, parent) {
|
||||
const results = state.all(node)
|
||||
const loose = parent ? listLoose(parent) : listItemLoose(node)
|
||||
/** @type {Properties} */
|
||||
const properties = {}
|
||||
/** @type {Array<ElementContent>} */
|
||||
const children = []
|
||||
|
||||
if (typeof node.checked === 'boolean') {
|
||||
const head = results[0]
|
||||
/** @type {Element} */
|
||||
let paragraph
|
||||
|
||||
if (head && head.type === 'element' && head.tagName === 'p') {
|
||||
paragraph = head
|
||||
} else {
|
||||
paragraph = {type: 'element', tagName: 'p', properties: {}, children: []}
|
||||
results.unshift(paragraph)
|
||||
}
|
||||
|
||||
if (paragraph.children.length > 0) {
|
||||
paragraph.children.unshift({type: 'text', value: ' '})
|
||||
}
|
||||
|
||||
paragraph.children.unshift({
|
||||
type: 'element',
|
||||
tagName: 'input',
|
||||
properties: {type: 'checkbox', checked: node.checked, disabled: true},
|
||||
children: []
|
||||
})
|
||||
|
||||
// According to github-markdown-css, this class hides bullet.
|
||||
// See: <https://github.com/sindresorhus/github-markdown-css>.
|
||||
properties.className = ['task-list-item']
|
||||
}
|
||||
|
||||
let index = -1
|
||||
|
||||
while (++index < results.length) {
|
||||
const child = results[index]
|
||||
|
||||
// Add eols before nodes, except if this is a loose, first paragraph.
|
||||
if (
|
||||
loose ||
|
||||
index !== 0 ||
|
||||
child.type !== 'element' ||
|
||||
child.tagName !== 'p'
|
||||
) {
|
||||
children.push({type: 'text', value: '\n'})
|
||||
}
|
||||
|
||||
if (child.type === 'element' && child.tagName === 'p' && !loose) {
|
||||
children.push(...child.children)
|
||||
} else {
|
||||
children.push(child)
|
||||
}
|
||||
}
|
||||
|
||||
const tail = results[results.length - 1]
|
||||
|
||||
// Add a final eol.
|
||||
if (tail && (loose || tail.type !== 'element' || tail.tagName !== 'p')) {
|
||||
children.push({type: 'text', value: '\n'})
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {type: 'element', tagName: 'li', properties, children}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Parents} node
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function listLoose(node) {
|
||||
let loose = false
|
||||
if (node.type === 'list') {
|
||||
loose = node.spread || false
|
||||
const children = node.children
|
||||
let index = -1
|
||||
|
||||
while (!loose && ++index < children.length) {
|
||||
loose = listItemLoose(children[index])
|
||||
}
|
||||
}
|
||||
|
||||
return loose
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ListItem} node
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function listItemLoose(node) {
|
||||
const spread = node.spread
|
||||
|
||||
return spread === null || spread === undefined
|
||||
? node.children.length > 1
|
||||
: spread
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* @import {Element, Properties} from 'hast'
|
||||
* @import {List} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `list` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {List} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function list(state, node) {
|
||||
/** @type {Properties} */
|
||||
const properties = {}
|
||||
const results = state.all(node)
|
||||
let index = -1
|
||||
|
||||
if (typeof node.start === 'number' && node.start !== 1) {
|
||||
properties.start = node.start
|
||||
}
|
||||
|
||||
// Like GitHub, add a class for custom styling.
|
||||
while (++index < results.length) {
|
||||
const child = results[index]
|
||||
|
||||
if (
|
||||
child.type === 'element' &&
|
||||
child.tagName === 'li' &&
|
||||
child.properties &&
|
||||
Array.isArray(child.properties.className) &&
|
||||
child.properties.className.includes('task-list-item')
|
||||
) {
|
||||
properties.className = ['contains-task-list']
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: node.ordered ? 'ol' : 'ul',
|
||||
properties,
|
||||
children: state.wrap(results, true)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {Paragraph} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `paragraph` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Paragraph} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function paragraph(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'p',
|
||||
properties: {},
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @import {Parents as HastParents, Root as HastRoot} from 'hast'
|
||||
* @import {Root as MdastRoot} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `root` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {MdastRoot} node
|
||||
* mdast node.
|
||||
* @returns {HastParents}
|
||||
* hast node.
|
||||
*/
|
||||
export function root(state, node) {
|
||||
/** @type {HastRoot} */
|
||||
const result = {type: 'root', children: state.wrap(state.all(node))}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {Strong} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `strong` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Strong} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function strong(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'strong',
|
||||
properties: {},
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {TableCell} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `tableCell` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {TableCell} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function tableCell(state, node) {
|
||||
// Note: this function is normally not called: see `table-row` for how rows
|
||||
// and their cells are compiled.
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'td', // Assume body cell.
|
||||
properties: {},
|
||||
children: state.all(node)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* @import {Element, ElementContent, Properties} from 'hast'
|
||||
* @import {Parents, TableRow} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `tableRow` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {TableRow} node
|
||||
* mdast node.
|
||||
* @param {Parents | undefined} parent
|
||||
* Parent of `node`.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function tableRow(state, node, parent) {
|
||||
const siblings = parent ? parent.children : undefined
|
||||
// Generate a body row when without parent.
|
||||
const rowIndex = siblings ? siblings.indexOf(node) : 1
|
||||
const tagName = rowIndex === 0 ? 'th' : 'td'
|
||||
// To do: option to use `style`?
|
||||
const align = parent && parent.type === 'table' ? parent.align : undefined
|
||||
const length = align ? align.length : node.children.length
|
||||
let cellIndex = -1
|
||||
/** @type {Array<ElementContent>} */
|
||||
const cells = []
|
||||
|
||||
while (++cellIndex < length) {
|
||||
// Note: can also be undefined.
|
||||
const cell = node.children[cellIndex]
|
||||
/** @type {Properties} */
|
||||
const properties = {}
|
||||
const alignValue = align ? align[cellIndex] : undefined
|
||||
|
||||
if (alignValue) {
|
||||
properties.align = alignValue
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
let result = {type: 'element', tagName, properties, children: []}
|
||||
|
||||
if (cell) {
|
||||
result.children = state.all(cell)
|
||||
state.patch(cell, result)
|
||||
result = state.applyData(cell, result)
|
||||
}
|
||||
|
||||
cells.push(result)
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'tr',
|
||||
properties: {},
|
||||
children: state.wrap(cells, true)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* @import {Table} from 'mdast'
|
||||
* @import {Element} from 'hast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
import {pointEnd, pointStart} from 'unist-util-position'
|
||||
|
||||
/**
|
||||
* Turn an mdast `table` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {Table} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function table(state, node) {
|
||||
const rows = state.all(node)
|
||||
const firstRow = rows.shift()
|
||||
/** @type {Array<Element>} */
|
||||
const tableContent = []
|
||||
|
||||
if (firstRow) {
|
||||
/** @type {Element} */
|
||||
const head = {
|
||||
type: 'element',
|
||||
tagName: 'thead',
|
||||
properties: {},
|
||||
children: state.wrap([firstRow], true)
|
||||
}
|
||||
state.patch(node.children[0], head)
|
||||
tableContent.push(head)
|
||||
}
|
||||
|
||||
if (rows.length > 0) {
|
||||
/** @type {Element} */
|
||||
const body = {
|
||||
type: 'element',
|
||||
tagName: 'tbody',
|
||||
properties: {},
|
||||
children: state.wrap(rows, true)
|
||||
}
|
||||
|
||||
const start = pointStart(node.children[1])
|
||||
const end = pointEnd(node.children[node.children.length - 1])
|
||||
if (start && end) body.position = {start, end}
|
||||
tableContent.push(body)
|
||||
}
|
||||
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'table',
|
||||
properties: {},
|
||||
children: state.wrap(tableContent, true)
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @import {Element as HastElement, Text as HastText} from 'hast'
|
||||
* @import {Text as MdastText} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
import {trimLines} from 'trim-lines'
|
||||
|
||||
/**
|
||||
* Turn an mdast `text` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {MdastText} node
|
||||
* mdast node.
|
||||
* @returns {HastElement | HastText}
|
||||
* hast node.
|
||||
*/
|
||||
export function text(state, node) {
|
||||
/** @type {HastText} */
|
||||
const result = {type: 'text', value: trimLines(String(node.value))}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @import {Element} from 'hast'
|
||||
* @import {ThematicBreak} from 'mdast'
|
||||
* @import {State} from '../state.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Turn an mdast `thematicBreak` node into hast.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around.
|
||||
* @param {ThematicBreak} node
|
||||
* mdast node.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
export function thematicBreak(state, node) {
|
||||
/** @type {Element} */
|
||||
const result = {
|
||||
type: 'element',
|
||||
tagName: 'hr',
|
||||
properties: {},
|
||||
children: []
|
||||
}
|
||||
state.patch(node, result)
|
||||
return state.applyData(node, result)
|
||||
}
|
||||
Reference in New Issue
Block a user