2022-08-10 10:06:31 -05:00
|
|
|
import { gfmFromMarkdown } from 'mdast-util-gfm'
|
|
|
|
import { visit } from 'unist-util-visit'
|
2023-07-25 13:59:07 -05:00
|
|
|
import { gfm } from 'micromark-extension-gfm'
|
|
|
|
import { fromMarkdown } from 'mdast-util-from-markdown'
|
2022-08-10 10:06:31 -05:00
|
|
|
|
|
|
|
export function mdHas (md, test) {
|
2023-07-25 13:32:48 -05:00
|
|
|
if (!md) return []
|
2022-08-10 10:06:31 -05:00
|
|
|
const tree = fromMarkdown(md, {
|
2023-07-25 13:59:07 -05:00
|
|
|
extensions: [gfm()],
|
2022-08-10 10:06:31 -05:00
|
|
|
mdastExtensions: [gfmFromMarkdown()]
|
|
|
|
})
|
|
|
|
|
|
|
|
let found = false
|
|
|
|
visit(tree, test, () => {
|
|
|
|
found = true
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
return found
|
|
|
|
}
|
2023-07-13 02:10:01 +02:00
|
|
|
|
|
|
|
export function extractUrls (md) {
|
|
|
|
if (!md) return []
|
|
|
|
const tree = fromMarkdown(md, {
|
2023-07-25 13:59:07 -05:00
|
|
|
extensions: [gfm()],
|
2023-07-13 02:10:01 +02:00
|
|
|
mdastExtensions: [gfmFromMarkdown()]
|
|
|
|
})
|
|
|
|
|
|
|
|
const urls = new Set()
|
|
|
|
visit(tree, ({ type }) => {
|
2023-07-13 15:18:04 -05:00
|
|
|
return type === 'link' || type === 'image'
|
2023-07-13 02:10:01 +02:00
|
|
|
}, ({ url }) => {
|
|
|
|
urls.add(url)
|
|
|
|
})
|
|
|
|
|
|
|
|
return Array.from(urls)
|
|
|
|
}
|