mdast#Code TypeScript Examples

The following examples show how to use mdast#Code. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From dendron with GNU Affero General Public License v3.0 6 votes vote down vote up
static isCode(node: Node): node is Code {
    return node.type === DendronASTTypes.CODE;
  }
Example #2
Source File: case.ts    From reskript with MIT License 6 votes vote down vote up
parseToCase = async ([heading, meta, ...nodes]: Content[]): Promise<PlayCase | null> => {
    if (heading.type !== 'heading' || heading.depth !== 2) {
        return null;
    }

    const replCodeBlock = findReplCodeBlock(nodes);

    if (!replCodeBlock) {
        return null;
    }

    const description = stringifyNodesToMarkdown(nodes.filter(v => v !== replCodeBlock));
    const metaInfo = await extractMeta(meta);
    return {
        name: (heading.children[0] as Text).value,
        description: description.trim(),
        code: (replCodeBlock as Code).value,
        ...metaInfo,
    };
}
Example #3
Source File: remark.ts    From vite-plugin-md-preview with MIT License 5 votes vote down vote up
export function remarkVue(options: RemarkVueOptions): Plugin {
  const { file, root, highlighter, remove, update } = options

  const resolve = (...args: string[]) => {
    let ret = path.resolve(path.dirname(file), ...args)
    ret = path.relative(root, ret)
    return `/${ret}`
  }
  function transformer(tree): Transformer {
    const oldBlocks = fileCodeMap.get(file) || []
    const blocks: CodeBlock[] = []
    visit(tree, 'code', (node: Code, i: number, parent: Parent) => {
      const attrs = (node.meta || '').split(' ').reduce((prev, curr) => {
        const [key, value] = curr.split('=')
        if (typeof value === 'undefined') {
          prev[key] = true
        } else {
          prev[key] = value
        }
        return prev
      }, {} as Record<string, string | boolean>)

      if (node.lang === 'vue' && attrs['preview']) {
        const name = `VueCode${md5(file).substr(0, 8)}I${i}`
        const component = typeof attrs['preview'] === 'string' ? attrs['preview'] : 'VueCode'
        const code = highlighter(node.value)
        blocks.push({ name, path: resolve(`./${name}.vue`), code: node.value })
        const demoNode: HTML = {
          type: 'html',
          value: `<${component} source="${encodeURIComponent(code)}">
  <${name} />
</${component}>`,
        }
        parent.children.splice(i, 1, demoNode)
      }
    })
    const names = blocks.map(i => i.name)
    remove(oldBlocks)
    fileCodeMap.set(file, names)
    update(blocks)

    const imports = names.reduce((prev, curr) => {
      return `${prev}import ${curr} from "${resolve(`./${curr}.vue`)}"\n`
    }, '')
    const script = `<script setup>\n${imports}</script>`
    tree.children.splice(0, 0, { type: 'html', value: script })
    return tree
  }

  return transformer
}