Skip to content

Commit

Permalink
Move to dist
Browse files Browse the repository at this point in the history
  • Loading branch information
igorskyflyer committed Jul 10, 2024
1 parent 85ee30f commit f26ab8c
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions dist/utils.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { fromMarkdown } from 'mdast-util-from-markdown'
import { mdxFromMarkdown } from 'mdast-util-mdx'
import { mdxjs } from 'micromark-extension-mdxjs'

export function isPriorV2(post: any): boolean {
return (
typeof post['compiledContent'] === 'function' &&
typeof post['rawContent'] === 'function'
)
}

export function isV2(post: any): boolean {
return typeof post['body'] === 'string'
}

export function isMdx(post: any): boolean {
return typeof post['Content'] === 'function'
}

function walk(node): string | undefined {
const allowedNodes: string[] = ['emphasis', 'paragraph', 'strong', 'text']

if (!allowedNodes.includes(node.type)) {
return
}

let result: string = ''

if (node.value?.length > 0) {
result += node.value
} else if (node.children?.length > 0) {
node.children.forEach((child) => {
if (allowedNodes.includes(child.type)) {
const data = walk(child)

if (data) {
result += data
}
}
})
}

return result
}

export function getPlainText(markdown: string): string {
let result: string = ''

const tree = fromMarkdown(markdown, {
extensions: [mdxjs()],
mdastExtensions: [mdxFromMarkdown()]
})

tree.children.forEach((node) => {
const data = walk(node)

if (data) {
result += data
}
})

return result
}

0 comments on commit f26ab8c

Please sign in to comment.