Skip to content

Commit

Permalink
feat(views): restrict compiling by nesting levels
Browse files Browse the repository at this point in the history
  • Loading branch information
stdword committed Mar 29, 2023
1 parent d5ba14b commit bfd5992
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/utils/mldoc_ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class LogseqMarkup {
this.context = context
}

async toHTML(text: string): Promise<string> {
async toHTML(text: string, nestingLevel: number = 0): Promise<string> {
const unparsedNodes: string = Mldoc.parseInlineJson(
text,
JSON.stringify(MLDOC_OPTIONS),
Expand All @@ -106,7 +106,7 @@ export class LogseqMarkup {
// data as string)
// })

return await new MldocASTtoHTMLCompiler(this.context).compile(nodes)
return await new MldocASTtoHTMLCompiler(this.context, nestingLevel).compile(nodes)
}

static _transformUnorderedListsToAsteriskNotation(text: string) {
Expand All @@ -116,10 +116,14 @@ export class LogseqMarkup {


class MldocASTtoHTMLCompiler {
static maxNestingLevelForInlineBlockRefs = 6 // value from Logseq

context: ILogseqContext
nestingLevel: number

constructor(context: ILogseqContext) {
constructor(context: ILogseqContext, nestingLevel: number = 0) {
this.context = context
this.nestingLevel = nestingLevel
}

async compile(ast: MLDOC_Node[]) {
Expand Down Expand Up @@ -186,9 +190,13 @@ class MldocASTtoHTMLCompiler {
label = `((...))` // self reference
else {
label = block.content.split('\n', 1)[0]
// NOTE: recursion
// TODO: catch cycle
label = await new LogseqMarkup(this.context).toHTML(label)
if (this.nestingLevel <= MldocASTtoHTMLCompiler.maxNestingLevelForInlineBlockRefs)
// NOTE: recursion
label = await new LogseqMarkup(this.context).toHTML(label, this.nestingLevel + 1)
else
label = html`
<span class="warning text-sm">Block reference nesting is too deep</span>
`
}
}

Expand Down

0 comments on commit bfd5992

Please sign in to comment.