ts-mdast v1.0.0
ts-mdast - Typescript utilities for Markdown Abstract Syntax Tree
This package provides utilities to work with Markdown abstract syntax tree (MDAST). It contains:
- functions to create the different node types (also useful in pure Javascript);
- type guard and assertion functions to ensure the node is of expected type (especially useful with Typescript).
This package also re-export all types of @types/mdast and type of @types/unist which are useful for Markdown. It is therefore not necessary to include those packages if you are using this one.
Language/langue
Documents, messages, code (including variable names and comments), are in English.
Anyway, because Slune is French firm, all documents and important messages must also be provided in French. Other translations are welcome.
:fr: Une version française de ce document se trouve ici.
Installation
Installation is done using npm install command:
$ npm install --save ts-mdastUsage
For each NodeType node type, the package contains :
- an
assertNodeType(node: Node)function raising an exception ifnodeis not of type NodeType ; - an
isNodeType(node: Node)function returningtrueifnodeis of type NodeType ; - a
createNodeType(...)function creating a node of type NodeType.
The first two functions are understood by Typescript which is after able to control the node type in an appropriate way.
The creation function is used this as following:
createNodeType(mandatory1, mandatory2 [, optional1 [, optional2 [, optional3]] | {optional1, optional2, optional3}] [, children])where mandatory_ represent the mandatory parameters, optional_ the optional parameters and children an array with child nodes. Following calls are therefore possible:
createNodeType(mandatory1, mandatory2, optional1, [child1, child2])
createNodeType(mandatory1, mandatory2, optional1, optional2)
createNodeType(mandatory1, mandatory2, { optional1, optional3 })
createNodeType(mandatory1, mandatory2, { optional2, optional3 }, [child1, child2])
createNodeType(mandatory1, mandatory2, [child1, child2])The array containing children is directly used in the node and can therefore be modified after node creation:
const children: Content[] = []
createNodeType(mandatory1, mandatory2, children)
children.push(createNodeType(mandatory1, mandatory2))Notes:
- The
createNodeType(...)function only exists for concrete nodes (not onContentnorBlockContentfor example). - All node types do not have mandatory parameters, optional parameters or child nodes.
Nodes
Parent
Reference: Parent
Type assertion:
function assertParent(node: Node): asserts node is ParentType guard:
function isParent(node: Node): node is ParentLiteral
Reference: Literal
Type assertion:
function assertLiteral(node: Node): asserts node is LiteralType guard:
function isLiteral(node: Node): node is LiteralContent
Reference: Content
Type assertion:
function assertContent(node: Node): asserts node is ContentType guard:
function isContent(node: Node): node is ContentTopLevelContent
Reference: TopLevelContent
Type assertion:
function assertTopLevelContent(node: Node): asserts node is TopLevelContentType guard:
function isTopLevelContent(node: Node): node is TopLevelContentBlockContent
Reference: BlockContent
Type assertion:
function assertBlockContent(node: Node): asserts node is BlockContentType guard:
function isBlockContent(node: Node): node is BlockContentFrontmatterContent
Reference: FrontmatterContent
Type assertion:
function assertFrontmatterContent(node: Node): asserts node is FrontmatterContentType guard:
function isFrontmatterContent(node: Node): node is FrontmatterContentDefinitionContent
Reference: DefinitionContent
Type assertion:
function assertDefinitionContent(node: Node): asserts node is DefinitionContentType guard:
function isDefinitionContent(node: Node): node is DefinitionContentListContent
Reference: ListContent
Type assertion:
function assertListContent(node: Node): asserts node is ListContentType guard:
function isListContent(node: Node): node is ListContentTableContent
Reference: TableContent
Type assertion:
function assertTableContent(node: Node): asserts node is TableContentType guard:
function isTableContent(node: Node): node is TableContentRowContent
Reference: RowContent
Type assertion:
function assertRowContent(node: Node): asserts node is RowContentType guard:
function isRowContent(node: Node): node is RowContentPhrasingContent
Reference: PhrasingContent
Type assertion:
function assertPhrasingContent(node: Node): asserts node is PhrasingContentType guard:
function isPhrasingContent(node: Node): node is PhrasingContentStaticPhrasingContent
Reference: StaticPhrasingContent
Type assertion:
function assertStaticPhrasingContent(node: Node): asserts node is StaticPhrasingContentType guard:
function isStaticPhrasingContent(node: Node): node is StaticPhrasingContentRoot
Reference: Root
Type assertion:
function assertRoot(node: Node): asserts node is RootType guard:
function isRoot(node: Node): node is RootCreation:
function createRoot(children?: Content[]): RootParagraph
Reference: Paragraph
Type assertion:
function assertParagraph(node: Node): asserts node is ParagraphType guard:
function isParagraph(node: Node): node is ParagraphCreation:
function createParagraph(children?: PhrasingContent[]): ParagraphHeading
Reference: Heading
Type assertion:
function assertHeading(node: Node): asserts node is HeadingType guard:
function isHeading(node: Node): node is HeadingCreation:
function createHeading(depth: 1 | 2 | 3 | 4 | 5 | 6, children?: PhrasingContent[])ThematicBreak
Reference: ThematicBreak
Type assertion:
function assertThematicBreak(node: Node): asserts node is ThematicBreakType guard:
function isThematicBreak(node: Node): node is ThematicBreakCreation:
function createThematicBreak(): ThematicBreakBlockquote
Reference: Blockquote
Type assertion:
function assertBlockquote(node: Node): asserts node is BlockquoteType guard:
function isBlockquote(node: Node): node is BlockquoteCreation:
function createBlockquote(children?: BlockContent[]): BlockquoteList
Reference: List
Type assertion:
function assertList(node: Node): asserts node is ListType guard:
function isList(node: Node): node is ListCreation:
function createList(ordered: boolean, start: number, spread: boolean, children?: ListContent[]): List
function createList(ordered: boolean, start: number, children?: ListContent[]): List
function createList(ordered: boolean, children?: ListContent[]): List
function createList(children?: ListContent[]): List
function createList(
options: { ordered?: boolean; start?: number; spread?: boolean },
children?: ListContent[]
): ListListItem
Reference: ListItem
Type assertion:
function assertListItem(node: Node): asserts node is ListItemType guard:
function isListItem(node: Node): node is ListItemCreation:
function createListItem(checked: boolean, spread: boolean, children?: BlockContent[]): ListItem
function createListItem(checked: boolean, children?: BlockContent[]): ListItem
function createListItem(children?: BlockContent[]): ListItem
function createListItem(
options: { checked?: boolean; spread?: boolean },
children?: BlockContent[]
): ListItemTable
Reference: Table
Type assertion:
function assertTable(node: Node): asserts node is TableType guard:
function isTable(node: Node): node is TableCreation:
function createTable(align: AlignType[], children?: TableContent[]): Table
function createTable(children?: TableContent[]): Table
function createTable(options: { align?: AlignType[] }, children?: TableContent[]): TableTableRow
Reference: TableRow
Type assertion:
function assertTableRow(node: Node): asserts node is TableRowType guard:
function isTableRow(node: Node): node is TableRowCreation:
function createTableRow(children?: RowContent[]): TableRowTableCell
Reference: TableCell
Type assertion:
function assertTableCell(node: Node): asserts node is TableCellType guard:
function isTableCell(node: Node): node is TableCellCreation:
function createTableCell(children?: PhrasingContent[]): TableCellHTML
Reference: HTML
Type assertion:
function assertHTML(node: Node): asserts node is HTMLType guard:
function isHTML(node: Node): node is HTMLCreation:
function createHTML(value: string): HTMLCode
Reference: Code
Type assertion:
function assertCode(node: Node): asserts node is CodeType guard:
function isCode(node: Node): node is CodeCreation:
function createCode(value: string, lang?: string, meta?: string): Code
function createCode(value: string, options: { lang?: string; meta?: string }): CodeYAML
Reference: YAML
Type assertion:
function assertYAML(node: Node): asserts node is YAMLType guard:
function isYAML(node: Node): node is YAMLCreation:
function createYAML(value: string): YAMLDefinition
Reference: Definition
Type assertion:
function assertDefinition(node: Node): asserts node is DefinitionType guard:
function isDefinition(node: Node): node is DefinitionCreation:
function createDefinition(identifier: string, url: string, label?: string, title?: string): Definition
function createDefinition(
identifier: string,
url: string,
options: { label?: string; title?: string }
): DefinitionFootnoteDefinition
Reference: FootnoteDefinition
Type assertion:
function assertFootnoteDefinition(node: Node): asserts node is FootnoteDefinitionType guard:
function isFootnoteDefinition(node: Node): node is FootnoteDefinitionCreation:
function createFootnoteDefinition(
identifier: string,
label: string,
children?: BlockContent[]
): FootnoteDefinition
function createFootnoteDefinition(identifier: string, children?: BlockContent[]): FootnoteDefinition
function createFootnoteDefinition(
identifier: string,
options: { label?: string },
children?: BlockContent[]
): FootnoteDefinitionText
Reference: Text
Type assertion:
function assertText(node: Node): asserts node is TextType guard:
function isText(node: Node): node is TextCreation:
function createText(value: string): TextEmphasis
Reference: Emphasis
Type assertion:
function assertEmphasis(node: Node): asserts node is EmphasisType guard:
function isEmphasis(node: Node): node is EmphasisCreation:
function createEmphasis(children?: PhrasingContent[]): EmphasisStrong
Reference: Strong
Type assertion:
function assertStrong(node: Node): asserts node is StrongType guard:
function isStrong(node: Node): node is StrongCreation:
function createStrong(children?: PhrasingContent[]): StrongDelete
Reference: Delete
Type assertion:
function assertDelete(node: Node): asserts node is DeleteType guard:
function isDelete(node: Node): node is DeleteCreation:
function createDelete(children?: PhrasingContent[]): DeleteInlineCode
Reference: InlineCode
Type assertion:
function assertInlineCode(node: Node): asserts node is InlineCodeType guard:
function isInlineCode(node: Node): node is InlineCodeCreation:
function createInlineCode(value: string): InlineCodeBreak
Reference: Break
Type assertion:
function assertBreak(node: Node): asserts node is BreakType guard:
function isBreak(node: Node): node is BreakCreation:
function createBreak(): BreakLink
Reference: Link
Type assertion:
function assertLink(node: Node): asserts node is LinkType guard:
function isLink(node: Node): node is LinkCreation:
function createLink(url: string, title: string, children?: StaticPhrasingContent[]): Link
function createLink(url: string, children?: StaticPhrasingContent[]): Link
function createLink(url: string, options: { title?: string }, children?: StaticPhrasingContent[]): LinkImage
Reference: Image
Type assertion:
function assertImage(node: Node): asserts node is ImageType guard:
function isImage(node: Node): node is ImageCreation:
function createImage(url: string, alt?: string, title?: string): Image
function createImage(url: string, options: { alt?: string; title?: string }): ImageLinkReference
Reference: LinkReference
Type assertion:
function assertLinkReference(node: Node): asserts node is LinkReferenceType guard:
function isLinkReference(node: Node): node is LinkReferenceCreation:
function createLinkReference(
identifier: string,
referenceType: ReferenceType,
label: string,
children?: StaticPhrasingContent[]
): LinkReference
function createLinkReference(
identifier: string,
referenceType: ReferenceType,
children?: StaticPhrasingContent[]
): LinkReference
function createLinkReference(
identifier: string,
referenceType: ReferenceType,
options: { label?: string },
children?: StaticPhrasingContent[]
): LinkReferenceImageReference
Reference: ImageReference
Type assertion:
function assertImageReference(node: Node): asserts node is ImageReferenceType guard:
function isImageReference(node: Node): node is ImageReferenceCreation:
function createImageReference(
identifier: string,
referenceType: ReferenceType,
alt?: string,
label?: string
): ImageReference
function createImageReference(
identifier: string,
referenceType: ReferenceType,
options: { alt?: string; label?: string }
): ImageReferenceFootnote
Reference: Footnote
Type assertion:
function assertFootnote(node: Node): asserts node is FootnoteType guard:
function isFootnote(node: Node): node is FootnoteCreation:
function createFootnote(children?: PhrasingContent[]): FootnoteFootnoteReference
Reference: FootnoteReference
Type assertion:
function assertFootnoteReference(node: Node): asserts node is FootnoteReferenceType guard:
function isFootnoteReference(node: Node): node is FootnoteReferenceCreation:
function createFootnoteReference(identifier: string, label?: string): FootnoteReference
function createFootnoteReference(identifier: string, options: { label?: string }): FootnoteReferenceContributing
Even though we cannot guarantee a response time, please feel free to file an issue if you have any question or problem using the package.
Pull Requests are welcome. You can, of course, submit corrections or improvements for code, but do not hesitate to also improve documentation, even for small spell or grammar errors.