stacker.news/lib/md.js

37 lines
756 B
JavaScript

import { fromMarkdown } from 'mdast-util-from-markdown'
import { gfmFromMarkdown } from 'mdast-util-gfm'
import { visit } from 'unist-util-visit'
import { gfm } from 'micromark-extension-gfm'
export function mdHas (md, test) {
const tree = fromMarkdown(md, {
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown()]
})
let found = false
visit(tree, test, () => {
found = true
return false
})
return found
}
export function extractUrls (md) {
if (!md) return []
const tree = fromMarkdown(md, {
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown()]
})
const urls = new Set()
visit(tree, ({ type }) => {
return type === 'link'
}, ({ url }) => {
urls.add(url)
})
return Array.from(urls)
}