Replies: 1 comment 19 replies
-
Hey again @ooker777! 👋 example of a pipeline that gets list itemsimport type {Root} from "mdast"
import {remark} from 'remark'
import remarkGfm from 'remark-gfm'
import {visit} from 'unist-util-visit'
const sourceMarkdown = `
## List
- One
- Two
## Tasklist
* [ ] to do
* [x] done
`
function remarkListPlugin () {
return (tree: Root) => {
visit(tree, "listItem", (node) => {
if (node.checked === null) {
// Do something with the list item nodes
console.log(node)
}
})
}
}
try {
// this renders the source to the page
document.querySelector('#source')!.textContent = sourceMarkdown
// this is the transformation pipeline
const file = await remark()
.use(remarkGfm)
.use(remarkListPlugin)
.process(sourceMarkdown)
// these render the output to the page
document.querySelector('#result')!.textContent = String(file)
document.querySelector('#error')!.textContent = ''
} catch (error) {
document.querySelector('#error')!.textContent = String(error)
} runnable example: https://codesandbox.io/p/devbox/remark-with-vite-rh4w48 |
Beta Was this translation helpful? Give feedback.
19 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
To select only
listItem
, I use:The output:
I notice that task list items have boolean
checked
, while regular list items have nullchecked
. So to select only task list items, I can use:But how can I only select regular list item? Either of these doesn't work:
I know I can work on the text, but that's cumbersome and defeats the purpose of the AST.
(Also asked on Stack Overflow)
Beta Was this translation helpful? Give feedback.
All reactions