123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import {zwitch} from 'zwitch'
- import {configure} from './configure.js'
- import {handle as handlers} from './handle/index.js'
- import {join} from './join.js'
- import {unsafe} from './unsafe.js'
- import {association} from './util/association.js'
- import {compilePattern} from './util/compile-pattern.js'
- import {containerPhrasing} from './util/container-phrasing.js'
- import {containerFlow} from './util/container-flow.js'
- import {indentLines} from './util/indent-lines.js'
- import {safe} from './util/safe.js'
- import {track} from './util/track.js'
- export function toMarkdown(tree, options = {}) {
-
- const state = {
- enter,
- indentLines,
- associationId: association,
- containerPhrasing: containerPhrasingBound,
- containerFlow: containerFlowBound,
- createTracker: track,
- compilePattern,
- safe: safeBound,
- stack: [],
- unsafe: [...unsafe],
- join: [...join],
-
-
- handlers: {...handlers},
- options: {},
- indexStack: [],
-
- handle: undefined
- }
- configure(state, options)
- if (state.options.tightDefinitions) {
- state.join.push(joinDefinition)
- }
- state.handle = zwitch('type', {
- invalid,
- unknown,
- handlers: state.handlers
- })
- let result = state.handle(tree, undefined, state, {
- before: '\n',
- after: '\n',
- now: {line: 1, column: 1},
- lineShift: 0
- })
- if (
- result &&
- result.charCodeAt(result.length - 1) !== 10 &&
- result.charCodeAt(result.length - 1) !== 13
- ) {
- result += '\n'
- }
- return result
-
- function enter(name) {
- state.stack.push(name)
- return exit
-
- function exit() {
- state.stack.pop()
- }
- }
- }
- function invalid(value) {
- throw new Error('Cannot handle value `' + value + '`, expected node')
- }
- function unknown(value) {
-
- const node = (value)
- throw new Error('Cannot handle unknown node `' + node.type + '`')
- }
- function joinDefinition(left, right) {
-
- if (left.type === 'definition' && left.type === right.type) {
- return 0
- }
- }
- function containerPhrasingBound(parent, info) {
- return containerPhrasing(parent, this, info)
- }
- function containerFlowBound(parent, info) {
- return containerFlow(parent, this, info)
- }
- function safeBound(value, config) {
- return safe(this, value, config)
- }
|