mirror of
https://github.com/danbulant/lezer-markdown-obsidian
synced 2026-05-24 12:35:48 +00:00
add ==mark== parsing
This commit is contained in:
parent
a49a0a39a0
commit
d95d1874ae
3 changed files with 69 additions and 16 deletions
|
|
@ -15,6 +15,10 @@ const specParser = new SpecParser(parser, {
|
||||||
cm: "CommentMarker",
|
cm: "CommentMarker",
|
||||||
EM: "Embed",
|
EM: "Embed",
|
||||||
eM: "EmbedMark",
|
eM: "EmbedMark",
|
||||||
|
FN: "Footnote",
|
||||||
|
fM: "FootnoteMark",
|
||||||
|
fL: "FootnoteLabel",
|
||||||
|
FR: "FootnoteReference",
|
||||||
H: "Hashtag",
|
H: "Hashtag",
|
||||||
hm: "HashtagMark",
|
hm: "HashtagMark",
|
||||||
hl: "HashtagLabel",
|
hl: "HashtagLabel",
|
||||||
|
|
@ -23,16 +27,14 @@ const specParser = new SpecParser(parser, {
|
||||||
iP: "InternalPath",
|
iP: "InternalPath",
|
||||||
iS: "InternalSubpath",
|
iS: "InternalSubpath",
|
||||||
iD: "InternalDisplay",
|
iD: "InternalDisplay",
|
||||||
FN: "Footnote",
|
M: "Mark",
|
||||||
fM: "FootnoteMark",
|
mm: "MarkMarker",
|
||||||
fL: "FootnoteLabel",
|
|
||||||
FR: "FootnoteReference",
|
|
||||||
YF: "YAMLFrontMatter",
|
|
||||||
ym: "YAMLMarker",
|
|
||||||
yc: "YAMLContent",
|
|
||||||
XB: "TexBlock",
|
XB: "TexBlock",
|
||||||
XI: "TexInline",
|
XI: "TexInline",
|
||||||
xm: "TexMarker",
|
xm: "TexMarker",
|
||||||
|
YF: "YAMLFrontMatter",
|
||||||
|
ym: "YAMLMarker",
|
||||||
|
yc: "YAMLContent",
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -192,6 +194,40 @@ Line 5}
|
||||||
Copyright (C) 2020 by Marijn Haverbeke <marijnh@gmail.com> and others
|
Copyright (C) 2020 by Marijn Haverbeke <marijnh@gmail.com> and others
|
||||||
https://github.com/lezer-parser/markdown/blob/f49eb8c8c82cfe45aa213ca1fe2cebc95305b88b/LICENSE
|
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(
|
test(
|
||||||
"Task list (in unordered list)",
|
"Task list (in unordered list)",
|
||||||
`
|
`
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ import {
|
||||||
} from "@lezer/markdown";
|
} from "@lezer/markdown";
|
||||||
|
|
||||||
declare module "@lezer/markdown" {
|
declare module "@lezer/markdown" {
|
||||||
class BlockContext {
|
interface BlockContext {
|
||||||
readonly input: Input;
|
readonly input: Input;
|
||||||
checkedYaml: boolean | null;
|
checkedYaml: boolean | null;
|
||||||
}
|
}
|
||||||
|
|
@ -24,7 +24,7 @@ export const Comment: MarkdownConfig = {
|
||||||
parseBlock: [
|
parseBlock: [
|
||||||
{
|
{
|
||||||
name: "CommentBlock",
|
name: "CommentBlock",
|
||||||
endLeaf: (cx, line: Line) => {
|
endLeaf: (_, line: Line) => {
|
||||||
return line.text.slice(line.pos, line.pos + 2) == "%%";
|
return line.text.slice(line.pos, line.pos + 2) == "%%";
|
||||||
},
|
},
|
||||||
parse(cx: BlockContext, line: Line) {
|
parse(cx: BlockContext, line: Line) {
|
||||||
|
|
@ -129,7 +129,7 @@ export const Footnote: MarkdownConfig = {
|
||||||
parseInline: [
|
parseInline: [
|
||||||
{
|
{
|
||||||
name: "Footnote",
|
name: "Footnote",
|
||||||
parse(cx: InlineContext, _: number, pos: number) {
|
parse(cx: InlineContext, _, pos: number) {
|
||||||
// typically [^1], but inside can match any characters but
|
// typically [^1], but inside can match any characters but
|
||||||
// square brackets and spaces.
|
// square brackets and spaces.
|
||||||
const match = /^\[\^[^\s[\]]+\]/.exec(cx.text.slice(pos - cx.offset));
|
const match = /^\[\^[^\s[\]]+\]/.exec(cx.text.slice(pos - cx.offset));
|
||||||
|
|
@ -151,7 +151,7 @@ export const Footnote: MarkdownConfig = {
|
||||||
parseBlock: [
|
parseBlock: [
|
||||||
{
|
{
|
||||||
name: "FootnoteReference",
|
name: "FootnoteReference",
|
||||||
leaf(cx: BlockContext, leaf: LeafBlock): LeafBlockParser | null {
|
leaf(_, leaf: LeafBlock): LeafBlockParser | null {
|
||||||
const ref = isFootnoteRef(leaf.content);
|
const ref = isFootnoteRef(leaf.content);
|
||||||
if (ref != -1) {
|
if (ref != -1) {
|
||||||
return new FootnoteReferenceParser(leaf.start + ref);
|
return new FootnoteReferenceParser(leaf.start + ref);
|
||||||
|
|
@ -176,7 +176,7 @@ export const Hashtag: MarkdownConfig = {
|
||||||
parseInline: [
|
parseInline: [
|
||||||
{
|
{
|
||||||
name: "Hashtag",
|
name: "Hashtag",
|
||||||
parse(cx, next, pos) {
|
parse(cx: InlineContext, next: number, pos: number) {
|
||||||
if (next != 35 /* # */) {
|
if (next != 35 /* # */) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
@ -211,7 +211,7 @@ export const InternalLink: MarkdownConfig = {
|
||||||
parseInline: [
|
parseInline: [
|
||||||
{
|
{
|
||||||
name: "InternalLink",
|
name: "InternalLink",
|
||||||
parse(cx: InlineContext, _: number, pos: number) {
|
parse(cx: InlineContext, _, pos: number) {
|
||||||
const el = parseInternalLink(cx, pos);
|
const el = parseInternalLink(cx, pos);
|
||||||
if (el) {
|
if (el) {
|
||||||
return cx.addElement(el);
|
return cx.addElement(el);
|
||||||
|
|
@ -339,6 +339,21 @@ function parseDisplay(cx: InlineContext, start: number): Element | null {
|
||||||
return 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 <marijnh@gmail.com> and others
|
Copyright (C) 2020 by Marijn Haverbeke <marijnh@gmail.com> and others
|
||||||
https://github.com/lezer-parser/markdown/blob/f49eb8c8c82cfe45aa213ca1fe2cebc95305b88b/LICENSE
|
https://github.com/lezer-parser/markdown/blob/f49eb8c8c82cfe45aa213ca1fe2cebc95305b88b/LICENSE
|
||||||
|
|
@ -370,7 +385,7 @@ export const TaskList: MarkdownConfig = {
|
||||||
parseBlock: [
|
parseBlock: [
|
||||||
{
|
{
|
||||||
name: "TaskList",
|
name: "TaskList",
|
||||||
leaf(cx, leaf) {
|
leaf(cx: BlockContext, leaf: LeafBlock) {
|
||||||
return /^\[.\]/.test(leaf.content) && cx.parentType().name == "ListItem"
|
return /^\[.\]/.test(leaf.content) && cx.parentType().name == "ListItem"
|
||||||
? new TaskParser()
|
? new TaskParser()
|
||||||
: null;
|
: null;
|
||||||
|
|
@ -428,7 +443,7 @@ export const Tex: MarkdownConfig = {
|
||||||
parseInline: [
|
parseInline: [
|
||||||
{
|
{
|
||||||
name: "TexInline",
|
name: "TexInline",
|
||||||
parse(cx: InlineContext, next: number, pos: number) {
|
parse(cx: InlineContext, _, pos: number) {
|
||||||
let match = /^\$(?:[^$\t ][^$]*)?[^$\t \\]\$(\D|$)/.exec(
|
let match = /^\$(?:[^$\t ][^$]*)?[^$\t \\]\$(\D|$)/.exec(
|
||||||
cx.text.slice(pos - cx.offset)
|
cx.text.slice(pos - cx.offset)
|
||||||
);
|
);
|
||||||
|
|
@ -453,7 +468,7 @@ export const YAMLFrontMatter: MarkdownConfig = {
|
||||||
parseBlock: [
|
parseBlock: [
|
||||||
{
|
{
|
||||||
name: "YAMLFrontMatter",
|
name: "YAMLFrontMatter",
|
||||||
parse(cx, line) {
|
parse(cx: BlockContext, line: Line) {
|
||||||
if (cx.checkedYaml) {
|
if (cx.checkedYaml) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -486,6 +501,7 @@ export const ObsidianMDExtensions = [
|
||||||
Footnote,
|
Footnote,
|
||||||
Hashtag,
|
Hashtag,
|
||||||
InternalLink,
|
InternalLink,
|
||||||
|
Mark,
|
||||||
Strikethrough,
|
Strikethrough,
|
||||||
Table,
|
Table,
|
||||||
TaskList,
|
TaskList,
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ export {
|
||||||
Footnote,
|
Footnote,
|
||||||
Hashtag,
|
Hashtag,
|
||||||
InternalLink,
|
InternalLink,
|
||||||
|
Mark,
|
||||||
ObsidianMDExtensions,
|
ObsidianMDExtensions,
|
||||||
parser,
|
parser,
|
||||||
TaskList,
|
TaskList,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue