-
Hey guys! I'm trying to figure out how to generate automatic IDs for Heading in order to do Anchor links. Here is an example of an override
So one way for example will be to add a the generated Any advice is appreciated! Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
What about the provided |
Beta Was this translation helpful? Give feedback.
-
I learnt this from plate's code export const getIdFactory = (tiddlerTitle: string): (() => string) => {
let id = 1;
return () => `${tiddlerTitle}-${id++}`;
};
const idCreator = useMemo(() => {
return getIdFactory(props.currentTiddler);
}, [props.currentTiddler]);
export function deserialize(input: string, options?: { idCreator?: () => string }): TNode[] {
return wikiAstToSlateAst(wikiAstFromWikiText(input), options);
}
export function wikiAstToSlateAst(node: IParseTreeNode | IParseTreeNode[], options?: { idCreator?: () => string }): TNode[] {
return convertNodes({ ...initialContext, ...options }, Array.isArray(node) ? node : [node]);
}
const withId = (nodeToAddId: TNode): TNode => (id === undefined ? nodeToAddId : { ...nodeToAddId, id });
if (node.type in context.builders) {
const builder = context.builders[node.type as keyof IContext['builders']];
if (typeof builder === 'function') {
// basic elements
const builtSlateNodeOrNodes = builder(context, node as never);
return Array.isArray(builtSlateNodeOrNodes)
? builtSlateNodeOrNodes.map((child) => withId({ ...getWikiASTAdditionalProperties(node), ...child }))
: ([withId({ ...getWikiASTAdditionalProperties(node), ...builtSlateNodeOrNodes })] as TNode[]);
}
} else {
// widget
// I guess this rule is enough for judge the current node is a widget? see `test/constants/wikiAst/widget.ts` for example.
if (typeof node.type === 'string' && 'tag' in node && typeof node.tag === 'string') {
const widgetNode = withId({ ...getWikiASTAdditionalProperties, ...context.builders.widget(context, node as ICustomParseTreeNode) } as TNode);
return [widgetNode];
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for your help,
|
Beta Was this translation helpful? Give feedback.
Thank you for your help,
I checked the createNodeIdPlugin but it was fitting my needs,
I found another way to do what I wanted to do, on my Headings Plugins I'm overriding the
props
to implement the generated ID inside theelement
example :