From d95d1874aea9aef168bcf5cf516331fec0a2a1a8 Mon Sep 17 00:00:00 2001 From: Eric Rykwalder Date: Fri, 11 Mar 2022 17:50:41 -0500 Subject: [PATCH] add ==mark== parsing --- __tests__/extensions.ts | 50 +++++++++++++++++++++++++++++++++++------ src/extensions.ts | 34 ++++++++++++++++++++-------- src/index.ts | 1 + 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/__tests__/extensions.ts b/__tests__/extensions.ts index cf24a28..5154e39 100644 --- a/__tests__/extensions.ts +++ b/__tests__/extensions.ts @@ -15,6 +15,10 @@ const specParser = new SpecParser(parser, { cm: "CommentMarker", EM: "Embed", eM: "EmbedMark", + FN: "Footnote", + fM: "FootnoteMark", + fL: "FootnoteLabel", + FR: "FootnoteReference", H: "Hashtag", hm: "HashtagMark", hl: "HashtagLabel", @@ -23,16 +27,14 @@ const specParser = new SpecParser(parser, { iP: "InternalPath", iS: "InternalSubpath", iD: "InternalDisplay", - FN: "Footnote", - fM: "FootnoteMark", - fL: "FootnoteLabel", - FR: "FootnoteReference", - YF: "YAMLFrontMatter", - ym: "YAMLMarker", - yc: "YAMLContent", + M: "Mark", + mm: "MarkMarker", XB: "TexBlock", XI: "TexInline", xm: "TexMarker", + YF: "YAMLFrontMatter", + ym: "YAMLMarker", + yc: "YAMLContent", }); /* @@ -192,6 +194,40 @@ Line 5} Copyright (C) 2020 by Marijn Haverbeke and others https://github.com/lezer-parser/markdown/blob/f49eb8c8c82cfe45aa213ca1fe2cebc95305b88b/LICENSE */ + test( + "Mark", + ` +{P:{M:{mm:==}Hi{mm:==}} Hello, world!}` + ); + + test( + "Mark 2", + ` +{P:This ==has a} + +{P:new paragraph==.}` + ); + + test( + "Mark (nested)", + ` +{P:Nesting {St:{e:**}with {M:{mm:==}emphasis{mm:==}}{e:**}}.}` + ); + + test( + "Mark (overlapping)", + ` +{P:One {St:{e:**}two ==three{e:**}} four==} + +{P:One {M:{mm:==}two **three{mm:==}} four**}` + ); + + test( + "Mark (escaped)", + ` +{P:A {Esc:\\=}=b c==}` + ); + test( "Task list (in unordered list)", ` diff --git a/src/extensions.ts b/src/extensions.ts index 37d78a5..789e072 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -13,7 +13,7 @@ import { } from "@lezer/markdown"; declare module "@lezer/markdown" { - class BlockContext { + interface BlockContext { readonly input: Input; checkedYaml: boolean | null; } @@ -24,7 +24,7 @@ export const Comment: MarkdownConfig = { parseBlock: [ { name: "CommentBlock", - endLeaf: (cx, line: Line) => { + endLeaf: (_, line: Line) => { return line.text.slice(line.pos, line.pos + 2) == "%%"; }, parse(cx: BlockContext, line: Line) { @@ -129,7 +129,7 @@ export const Footnote: MarkdownConfig = { parseInline: [ { name: "Footnote", - parse(cx: InlineContext, _: number, pos: number) { + parse(cx: InlineContext, _, pos: number) { // typically [^1], but inside can match any characters but // square brackets and spaces. const match = /^\[\^[^\s[\]]+\]/.exec(cx.text.slice(pos - cx.offset)); @@ -151,7 +151,7 @@ export const Footnote: MarkdownConfig = { parseBlock: [ { name: "FootnoteReference", - leaf(cx: BlockContext, leaf: LeafBlock): LeafBlockParser | null { + leaf(_, leaf: LeafBlock): LeafBlockParser | null { const ref = isFootnoteRef(leaf.content); if (ref != -1) { return new FootnoteReferenceParser(leaf.start + ref); @@ -176,7 +176,7 @@ export const Hashtag: MarkdownConfig = { parseInline: [ { name: "Hashtag", - parse(cx, next, pos) { + parse(cx: InlineContext, next: number, pos: number) { if (next != 35 /* # */) { return -1; } @@ -211,7 +211,7 @@ export const InternalLink: MarkdownConfig = { parseInline: [ { name: "InternalLink", - parse(cx: InlineContext, _: number, pos: number) { + parse(cx: InlineContext, _, pos: number) { const el = parseInternalLink(cx, pos); if (el) { return cx.addElement(el); @@ -339,6 +339,21 @@ function parseDisplay(cx: InlineContext, start: number): Element | null { return null; } +const MarkDelim = { resolve: "Mark", mark: "MarkMarker" }; + +export const Mark: MarkdownConfig = { + defineNodes: ["Mark", "MarkMarker"], + parseInline: [ + { + name: "Mark", + parse(cx: InlineContext, next: number, pos: number) { + if (next != 61 /* '=' */ || cx.char(pos + 1) != 61) return -1; + return cx.addDelimiter(MarkDelim, pos, pos + 2, true, true); + }, + }, + ], +}; + /* Copyright (C) 2020 by Marijn Haverbeke and others https://github.com/lezer-parser/markdown/blob/f49eb8c8c82cfe45aa213ca1fe2cebc95305b88b/LICENSE @@ -370,7 +385,7 @@ export const TaskList: MarkdownConfig = { parseBlock: [ { name: "TaskList", - leaf(cx, leaf) { + leaf(cx: BlockContext, leaf: LeafBlock) { return /^\[.\]/.test(leaf.content) && cx.parentType().name == "ListItem" ? new TaskParser() : null; @@ -428,7 +443,7 @@ export const Tex: MarkdownConfig = { parseInline: [ { name: "TexInline", - parse(cx: InlineContext, next: number, pos: number) { + parse(cx: InlineContext, _, pos: number) { let match = /^\$(?:[^$\t ][^$]*)?[^$\t \\]\$(\D|$)/.exec( cx.text.slice(pos - cx.offset) ); @@ -453,7 +468,7 @@ export const YAMLFrontMatter: MarkdownConfig = { parseBlock: [ { name: "YAMLFrontMatter", - parse(cx, line) { + parse(cx: BlockContext, line: Line) { if (cx.checkedYaml) { return false; } @@ -486,6 +501,7 @@ export const ObsidianMDExtensions = [ Footnote, Hashtag, InternalLink, + Mark, Strikethrough, Table, TaskList, diff --git a/src/index.ts b/src/index.ts index 0c3c665..33cee51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ export { Footnote, Hashtag, InternalLink, + Mark, ObsidianMDExtensions, parser, TaskList,