mirror of
https://github.com/danbulant/lezer-markdown-obsidian
synced 2026-05-19 04:18:46 +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",
|
||||
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 <marijnh@gmail.com> 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)",
|
||||
`
|
||||
|
|
|
|||
|
|
@ -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 <marijnh@gmail.com> 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,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ export {
|
|||
Footnote,
|
||||
Hashtag,
|
||||
InternalLink,
|
||||
Mark,
|
||||
ObsidianMDExtensions,
|
||||
parser,
|
||||
TaskList,
|
||||
|
|
|
|||
Loading…
Reference in a new issue