Compare commits

..

2 commits
0.0.3 ... main

Author SHA1 Message Date
Eric Rykwalder
6223674c53
Add MIT License
Closes #1
2023-12-27 08:21:27 -05:00
Eric Rykwalder
47bed7489e handle intermixed parsing for tex and comments 2022-03-11 18:52:43 -05:00
2 changed files with 42 additions and 24 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Eric Rykwalder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -19,6 +19,8 @@ declare module "@lezer/markdown" {
} }
} }
const CommentDelim = { resolve: "Comment", mark: "CommentMarker" };
export const Comment: MarkdownConfig = { export const Comment: MarkdownConfig = {
defineNodes: ["Comment", "CommentMarker"], defineNodes: ["Comment", "CommentMarker"],
parseBlock: [ parseBlock: [
@ -66,18 +68,17 @@ export const Comment: MarkdownConfig = {
{ {
name: "CommentInline", name: "CommentInline",
parse(cx: InlineContext, next: number, pos: number) { parse(cx: InlineContext, next: number, pos: number) {
let match = /^%%[^\n]*[^\n\\]%%/.exec(cx.text.slice(pos - cx.offset)); if (next == 37 && cx.char(pos + 1) == 37) {
if (!match) { let canClose = true;
return -1; if (
cx.slice(cx.offset, pos).lastIndexOf("\n") >
cx.slice(cx.offset, pos).lastIndexOf("%%")
) {
canClose = false;
}
return cx.addDelimiter(CommentDelim, pos, pos + 2, true, canClose);
} }
const start = pos; return -1;
const end = pos + match[0].length;
return cx.addElement(
cx.elt("Comment", start, end, [
cx.elt("CommentMarker", start, start + 2),
cx.elt("CommentMarker", end - 2, end),
])
);
}, },
}, },
], ],
@ -396,6 +397,8 @@ export const TaskList: MarkdownConfig = {
}; };
/* End Copyright */ /* End Copyright */
const TexDelim = { resolve: "TexInline", mark: "TexMarker" };
export const Tex: MarkdownConfig = { export const Tex: MarkdownConfig = {
defineNodes: ["TexBlock", "TexInline", "TexMarker"], defineNodes: ["TexBlock", "TexInline", "TexMarker"],
parseBlock: [ parseBlock: [
@ -443,21 +446,15 @@ export const Tex: MarkdownConfig = {
parseInline: [ parseInline: [
{ {
name: "TexInline", name: "TexInline",
parse(cx: InlineContext, _, pos: number) { parse(cx: InlineContext, next: number, pos: number) {
let match = /^\$(?:[^$\t ][^$]*)?[^$\t \\]\$(\D|$)/.exec( if (next != 36 /* $ */) {
cx.text.slice(pos - cx.offset)
);
if (!match) {
return -1; return -1;
} }
const start = pos; const before = cx.slice(pos - 1, pos);
const end = start + match[0].length - match[1].length; const after = cx.slice(pos + 1, pos + 2);
return cx.addElement( const canClose = /[^ \t]/.test(before) && !/\d/.test(after);
cx.elt("TexInline", start, end, [ const canOpen = /[^$ \t]/.test(after);
cx.elt("TexMarker", start, start + 1), return cx.addDelimiter(TexDelim, pos, pos + 1, canOpen, canClose);
cx.elt("TexMarker", end - 1, end),
])
);
}, },
}, },
], ],