Compare commits

..

No commits in common. "main" and "0.0.1" have entirely different histories.
main ... 0.0.1

7 changed files with 44 additions and 243 deletions

21
LICENSE
View file

@ -1,21 +0,0 @@
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.

125
README.md
View file

@ -1,125 +0,0 @@
# lezer-markdown-obsidian
This package is a set of extensions for [@lezer/markdown](https://github.com/lezer-parser/markdown) to add support for the Obsidian's added markdown syntax.
**Warning: This is not the parser that Obsidian itself uses. All parsing is a best attempt to match the way Obsidian parses markdown, and will not guarantee a 1-for-1 replication.**
If you recognize a difference in parsing, please open an issue.
## `parser`
The simplest was to use the library if you want full obsidian parsing is to import the parser:
```typescript
import { parser } from "lezer-markdown-obsidian";
const tree = parser.parse("# Some Markdown");
```
This parser includes all the extensions below, as well as the `Strikethrough` and `Table` extensions from `@lezer/markdown`.
## Extensions
You can configure your own parser with specific extensions:
```typescript
import { parser as mdParser } from "@lezer/markdown";
import { Comment, InternalLink } from "lezer-markdown-obsidian";
const parser = mdParser.configure([Comment, InternalLink]);
```
### `Extensions`
An array of all the extensions for Obsidian's markdown syntax.
### `BlockAndInline`
An array of all the extensions except YAML frontmatter.
### `Comment`
This adds support for parsing comments in the form of: `%%comment%%`.
Comments that begin at the start of a line can span multiple lines, and will go to the end of the document unless terminated.
Comments that are inline must be completed on the same line.
### `Footnote`
This adds support for detecting footnotes and footnote references.
Footnotes are in the form of:
`This has a footnote.[^1]`
References are in the form of:
`[^1]: Here is some additional info.`
References can span multiple lines as long as they are not interrupted by another block.
### `Hashtag`
This adds support for hashtags. Hashtags are pretty flexible in what can be tagged, only forbidding certain special characters.
`#this-is-a-tag`
`#nested/tag`
### `InternalLink`
This adds support for internal links and embeds.
Internal links are structured like:
`[[File#heading|display]]`
Internal embeds are structured like:
`![[File#heading]]`
The `#heading` and `|display` parts are optional. Heading can be a `#^blockid` instead, and multiple headings can be chained together.
### `Mark`
This adds support for highlight marks in the form of `==highlighted==`.
### `TaskList`
This adds support for Obsidian's task lists, which allow support for arbitrary characters for tasks. This makes it different from GFM task lists.
Open tasks are in the form of:
`- [ ] This is an uncompleted task`
Completed tasks are in the form of:
`- [x] This is a completed task`
Special tasks can replace `x` with any character in the completed form.
### `Tex`
This adds support for LaTex style formulas, both inline and block level. In Obsidian, these are rendered with MathJax.
Inline is in the form of:
`Here is some math: $1 + 2 = 3$`
Block level is in the form of:
```
$$
\vec v = \vec a t
$$
```
**Warning: Obsidian can also parse blocks as inline elements, which is currently not supported.**
### `YAMLFrontmatter`
This adds support for a frontmatter block of YAML. The frontmatter must be the first block in the document, otherwise it is treated as markdown. The YAML must be surrounded by lines with `---`.
For example:
```
---
author: Eric
---
```
**If you are parsing subselections of a document, you will want to configure a parser that does not include `YAMLFrontmatter`, since it will be unable to distinguise horizontal rules from frontmatter.**

View file

@ -15,10 +15,6 @@ 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",
@ -27,14 +23,16 @@ const specParser = new SpecParser(parser, {
iP: "InternalPath",
iS: "InternalSubpath",
iD: "InternalDisplay",
M: "Mark",
mm: "MarkMarker",
XB: "TexBlock",
XI: "TexInline",
xm: "TexMarker",
FN: "Footnote",
fM: "FootnoteMark",
fL: "FootnoteLabel",
FR: "FootnoteReference",
YF: "YAMLFrontMatter",
ym: "YAMLMarker",
yc: "YAMLContent",
XB: "TexBlock",
XI: "TexInline",
xm: "TexMarker",
});
/*
@ -194,40 +192,6 @@ 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)",
`

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "lezer-markdown-obsidian",
"version": "0.0.3",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "lezer-markdown-obsidian",
"version": "0.0.3",
"version": "0.0.1",
"license": "MIT",
"dependencies": {
"@lezer/common": "^0.15.11",

View file

@ -1,7 +1,7 @@
{
"name": "lezer-markdown-obsidian",
"description": "Obsidian Markdown extensions for @lezer/markdown",
"version": "0.0.3",
"version": "0.0.1",
"main": "dist/index.cjs",
"module": "dist/index.js",
"typings": "dist/index.d.ts",

View file

@ -13,20 +13,18 @@ import {
} from "@lezer/markdown";
declare module "@lezer/markdown" {
interface BlockContext {
class BlockContext {
readonly input: Input;
checkedYaml: boolean | null;
}
}
const CommentDelim = { resolve: "Comment", mark: "CommentMarker" };
export const Comment: MarkdownConfig = {
defineNodes: ["Comment", "CommentMarker"],
parseBlock: [
{
name: "CommentBlock",
endLeaf: (_, line: Line) => {
endLeaf: (cx, line: Line) => {
return line.text.slice(line.pos, line.pos + 2) == "%%";
},
parse(cx: BlockContext, line: Line) {
@ -68,17 +66,18 @@ export const Comment: MarkdownConfig = {
{
name: "CommentInline",
parse(cx: InlineContext, next: number, pos: number) {
if (next == 37 && cx.char(pos + 1) == 37) {
let canClose = true;
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);
let match = /^%%[^\n]*[^\n\\]%%/.exec(cx.text.slice(pos - cx.offset));
if (!match) {
return -1;
}
return -1;
const start = pos;
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),
])
);
},
},
],
@ -130,7 +129,7 @@ export const Footnote: MarkdownConfig = {
parseInline: [
{
name: "Footnote",
parse(cx: InlineContext, _, pos: number) {
parse(cx: InlineContext, _: number, 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));
@ -152,7 +151,7 @@ export const Footnote: MarkdownConfig = {
parseBlock: [
{
name: "FootnoteReference",
leaf(_, leaf: LeafBlock): LeafBlockParser | null {
leaf(cx: BlockContext, leaf: LeafBlock): LeafBlockParser | null {
const ref = isFootnoteRef(leaf.content);
if (ref != -1) {
return new FootnoteReferenceParser(leaf.start + ref);
@ -177,7 +176,7 @@ export const Hashtag: MarkdownConfig = {
parseInline: [
{
name: "Hashtag",
parse(cx: InlineContext, next: number, pos: number) {
parse(cx, next, pos) {
if (next != 35 /* # */) {
return -1;
}
@ -212,7 +211,7 @@ export const InternalLink: MarkdownConfig = {
parseInline: [
{
name: "InternalLink",
parse(cx: InlineContext, _, pos: number) {
parse(cx: InlineContext, _: number, pos: number) {
const el = parseInternalLink(cx, pos);
if (el) {
return cx.addElement(el);
@ -340,21 +339,6 @@ function parseDisplay(cx: InlineContext, start: number): Element | null {
return null;
}
export 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
@ -386,7 +370,7 @@ export const TaskList: MarkdownConfig = {
parseBlock: [
{
name: "TaskList",
leaf(cx: BlockContext, leaf: LeafBlock) {
leaf(cx, leaf) {
return /^\[.\]/.test(leaf.content) && cx.parentType().name == "ListItem"
? new TaskParser()
: null;
@ -397,8 +381,6 @@ export const TaskList: MarkdownConfig = {
};
/* End Copyright */
const TexDelim = { resolve: "TexInline", mark: "TexMarker" };
export const Tex: MarkdownConfig = {
defineNodes: ["TexBlock", "TexInline", "TexMarker"],
parseBlock: [
@ -447,14 +429,20 @@ export const Tex: MarkdownConfig = {
{
name: "TexInline",
parse(cx: InlineContext, next: number, pos: number) {
if (next != 36 /* $ */) {
let match = /^\$(?:[^$\t ][^$]*)?[^$\t \\]\$(\D|$)/.exec(
cx.text.slice(pos - cx.offset)
);
if (!match) {
return -1;
}
const before = cx.slice(pos - 1, pos);
const after = cx.slice(pos + 1, pos + 2);
const canClose = /[^ \t]/.test(before) && !/\d/.test(after);
const canOpen = /[^$ \t]/.test(after);
return cx.addDelimiter(TexDelim, pos, pos + 1, canOpen, canClose);
const start = pos;
const end = start + match[0].length - match[1].length;
return cx.addElement(
cx.elt("TexInline", start, end, [
cx.elt("TexMarker", start, start + 1),
cx.elt("TexMarker", end - 1, end),
])
);
},
},
],
@ -465,7 +453,7 @@ export const YAMLFrontMatter: MarkdownConfig = {
parseBlock: [
{
name: "YAMLFrontMatter",
parse(cx: BlockContext, line: Line) {
parse(cx, line) {
if (cx.checkedYaml) {
return false;
}
@ -493,12 +481,11 @@ export const YAMLFrontMatter: MarkdownConfig = {
],
};
export const Extensions = [
export const ObsidianMDExtensions = [
Comment,
Footnote,
Hashtag,
InternalLink,
Mark,
Strikethrough,
Table,
TaskList,
@ -506,6 +493,4 @@ export const Extensions = [
YAMLFrontMatter,
];
export const BlockAndInline = Extensions.slice(0, -1);
export const parser = defParser.configure(Extensions);
export const parser = defParser.configure(ObsidianMDExtensions);

View file

@ -1,11 +1,9 @@
export {
BlockAndInline,
Comment,
Extensions,
Footnote,
Hashtag,
InternalLink,
Mark,
ObsidianMDExtensions,
parser,
TaskList,
Tex,