mirror of
https://github.com/danbulant/lezer-markdown-obsidian
synced 2026-06-21 23:52:08 +00:00
Compare commits
7 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6223674c53 | ||
|
|
47bed7489e | ||
|
|
7ed8ebf669 | ||
|
|
d81022e19b | ||
|
|
549860b85b | ||
|
|
d95d1874ae | ||
|
|
a49a0a39a0 |
7 changed files with 243 additions and 44 deletions
21
LICENSE
Normal file
21
LICENSE
Normal 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.
|
||||
125
README.md
Normal file
125
README.md
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
# 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.**
|
||||
|
|
@ -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)",
|
||||
`
|
||||
|
|
|
|||
4
package-lock.json
generated
4
package-lock.json
generated
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "lezer-markdown-obsidian",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.3",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "lezer-markdown-obsidian",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.3",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@lezer/common": "^0.15.11",
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "lezer-markdown-obsidian",
|
||||
"description": "Obsidian Markdown extensions for @lezer/markdown",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.3",
|
||||
"main": "dist/index.cjs",
|
||||
"module": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
|
|
|
|||
|
|
@ -13,18 +13,20 @@ import {
|
|||
} from "@lezer/markdown";
|
||||
|
||||
declare module "@lezer/markdown" {
|
||||
class BlockContext {
|
||||
interface BlockContext {
|
||||
readonly input: Input;
|
||||
checkedYaml: boolean | null;
|
||||
}
|
||||
}
|
||||
|
||||
const CommentDelim = { resolve: "Comment", mark: "CommentMarker" };
|
||||
|
||||
export const Comment: MarkdownConfig = {
|
||||
defineNodes: ["Comment", "CommentMarker"],
|
||||
parseBlock: [
|
||||
{
|
||||
name: "CommentBlock",
|
||||
endLeaf: (cx, line: Line) => {
|
||||
endLeaf: (_, line: Line) => {
|
||||
return line.text.slice(line.pos, line.pos + 2) == "%%";
|
||||
},
|
||||
parse(cx: BlockContext, line: Line) {
|
||||
|
|
@ -66,18 +68,17 @@ export const Comment: MarkdownConfig = {
|
|||
{
|
||||
name: "CommentInline",
|
||||
parse(cx: InlineContext, next: number, pos: number) {
|
||||
let match = /^%%[^\n]*[^\n\\]%%/.exec(cx.text.slice(pos - cx.offset));
|
||||
if (!match) {
|
||||
return -1;
|
||||
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);
|
||||
}
|
||||
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),
|
||||
])
|
||||
);
|
||||
return -1;
|
||||
},
|
||||
},
|
||||
],
|
||||
|
|
@ -129,7 +130,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 +152,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 +177,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 +212,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 +340,21 @@ 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
|
||||
|
|
@ -370,7 +386,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;
|
||||
|
|
@ -381,6 +397,8 @@ export const TaskList: MarkdownConfig = {
|
|||
};
|
||||
/* End Copyright */
|
||||
|
||||
const TexDelim = { resolve: "TexInline", mark: "TexMarker" };
|
||||
|
||||
export const Tex: MarkdownConfig = {
|
||||
defineNodes: ["TexBlock", "TexInline", "TexMarker"],
|
||||
parseBlock: [
|
||||
|
|
@ -429,20 +447,14 @@ export const Tex: MarkdownConfig = {
|
|||
{
|
||||
name: "TexInline",
|
||||
parse(cx: InlineContext, next: number, pos: number) {
|
||||
let match = /^\$(?:[^$\t ][^$]*)?[^$\t \\]\$(\D|$)/.exec(
|
||||
cx.text.slice(pos - cx.offset)
|
||||
);
|
||||
if (!match) {
|
||||
if (next != 36 /* $ */) {
|
||||
return -1;
|
||||
}
|
||||
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),
|
||||
])
|
||||
);
|
||||
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);
|
||||
},
|
||||
},
|
||||
],
|
||||
|
|
@ -453,7 +465,7 @@ export const YAMLFrontMatter: MarkdownConfig = {
|
|||
parseBlock: [
|
||||
{
|
||||
name: "YAMLFrontMatter",
|
||||
parse(cx, line) {
|
||||
parse(cx: BlockContext, line: Line) {
|
||||
if (cx.checkedYaml) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -481,11 +493,12 @@ export const YAMLFrontMatter: MarkdownConfig = {
|
|||
],
|
||||
};
|
||||
|
||||
export const ObsidianMDExtensions = [
|
||||
export const Extensions = [
|
||||
Comment,
|
||||
Footnote,
|
||||
Hashtag,
|
||||
InternalLink,
|
||||
Mark,
|
||||
Strikethrough,
|
||||
Table,
|
||||
TaskList,
|
||||
|
|
@ -493,4 +506,6 @@ export const ObsidianMDExtensions = [
|
|||
YAMLFrontMatter,
|
||||
];
|
||||
|
||||
export const parser = defParser.configure(ObsidianMDExtensions);
|
||||
export const BlockAndInline = Extensions.slice(0, -1);
|
||||
|
||||
export const parser = defParser.configure(Extensions);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
export {
|
||||
BlockAndInline,
|
||||
Comment,
|
||||
Extensions,
|
||||
Footnote,
|
||||
Hashtag,
|
||||
InternalLink,
|
||||
ObsidianMDExtensions,
|
||||
Mark,
|
||||
parser,
|
||||
TaskList,
|
||||
Tex,
|
||||
|
|
|
|||
Loading…
Reference in a new issue