mdast#ListItem TypeScript Examples
The following examples show how to use
mdast#ListItem.
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: toc.ts From website-docs with MIT License | 6 votes |
export function mdxAstToToc(ast: ListItem[], config: PathConfig): RepoNav {
return ast.map(node => {
const content = node.children as [Paragraph, List | undefined]
if (content.length > 0 && content.length <= 2) {
const ret = getContentFromLink(content[0], config)
if (content[1]) {
const list = content[1]
if (list.type !== 'list') {
throw new Error(`incorrect listitem in TOC.md`)
}
ret.children = mdxAstToToc(list.children, config)
}
return ret
}
throw new Error(`incorrect format in TOC.md`)
})
}
Example #2
Source File: utils.ts From dendron with GNU Affero General Public License v3.0 | 5 votes |
/** Extract all blocks from the note which could be referenced by a block anchor.
*
* If those blocks already have anchors (or if they are a header), this will also find that anchor.
*
* @param note The note from which blocks will be extracted.
*/
static async extractBlocks({
note,
engine,
}: {
note: NoteProps;
engine: DEngineClient;
}): Promise<NoteBlock[]> {
const proc = MDUtilsV5.procRemarkFull({
engine,
vault: note.vault,
fname: note.fname,
dest: DendronASTDest.MD_DENDRON,
});
const slugger = getSlugger();
// Read and parse the note
const noteText = NoteUtils.serialize(note);
const noteAST = proc.parse(noteText);
// @ts-ignore
if (_.isUndefined(noteAST.children)) return [];
// @ts-ignore
const nodesToSearch = _.filter(noteAST.children as Node[], (node) =>
_.includes(NODE_TYPES_TO_EXTRACT, node.type)
);
// Extract the blocks
const blocks: NoteBlock[] = [];
for (const node of nodesToSearch) {
// Block anchors at top level refer to the blocks before them
if (node.type === DendronASTTypes.PARAGRAPH) {
// These look like a paragraph...
const parent = node as Paragraph;
if (parent.children.length === 1) {
// ... that has only a block anchor in it ...
const child = parent.children[0] as Node;
if (child.type === DendronASTTypes.BLOCK_ANCHOR) {
// ... in which case this block anchor refers to the previous block, if any
const previous = _.last(blocks);
if (!_.isUndefined(previous))
[, previous.anchor] =
AnchorUtils.anchorNode2anchor(child as BlockAnchor, slugger) ||
[];
// Block anchors themselves are not blocks, don't extract them
continue;
}
}
}
// Extract list items out of lists. We also extract them from nested lists,
// because block anchors can't refer to nested lists, only items inside of them
if (node.type === DendronASTTypes.LIST) {
visit(node, [DendronASTTypes.LIST_ITEM], (listItem: ListItem) => {
// The list item might have a block anchor inside of it.
let anchor: DNoteAnchorPositioned | undefined;
visit(
listItem,
[DendronASTTypes.BLOCK_ANCHOR, DendronASTTypes.LIST],
(inListItem) => {
// Except if we hit a nested list, because then the block anchor refers to the item in the nested list
if (inListItem.type === DendronASTTypes.LIST) return "skip";
[, anchor] =
AnchorUtils.anchorNode2anchor(
inListItem as BlockAnchor,
slugger
) || [];
return;
}
);
blocks.push({
text: proc.stringify(listItem),
anchor,
// position can only be undefined for generated nodes, not for parsed ones
position: listItem.position!,
type: listItem.type,
});
});
}
// extract the anchor for this block, if it exists
let anchor: DNoteAnchorPositioned | undefined;
if (node.type === DendronASTTypes.HEADING) {
// Headings are anchors themselves
[, anchor] =
AnchorUtils.anchorNode2anchor(node as Heading, slugger) || [];
} else if (node.type !== DendronASTTypes.LIST) {
// Other nodes might have block anchors inside them
// Except lists, because anchors inside lists only refer to specific list items
visit(node, [DendronASTTypes.BLOCK_ANCHOR], (child) => {
[, anchor] =
AnchorUtils.anchorNode2anchor(child as BlockAnchor, slugger) || [];
});
}
// extract the block
blocks.push({
text: proc.stringify(node),
anchor,
// position can only be undefined for generated nodes, not for parsed ones
position: node.position!,
type: node.type,
});
}
return blocks;
}