You've already forked obsidian-visualiser
New CM6 live edition components and floating cache and persistance.
This commit is contained in:
57
shared/grammar/callout.extension.ts
Normal file
57
shared/grammar/callout.extension.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { MarkdownConfig } from '@lezer/markdown';
|
||||
import { styleTags, tags } from '@lezer/highlight';
|
||||
|
||||
export const callout: MarkdownConfig = {
|
||||
defineNodes: [
|
||||
'CalloutBlock',
|
||||
'CalloutMarker',
|
||||
'CalloutMark',
|
||||
'CalloutType',
|
||||
'CalloutTitle',
|
||||
'CalloutLine',
|
||||
'CalloutContent',
|
||||
],
|
||||
parseBlock: [{
|
||||
name: 'Callout',
|
||||
before: 'Blockquote',
|
||||
parse(cx, line) {
|
||||
const match = /^>\s*\[!(\w+)\](?:\s+(.*))?/.exec(line.text);
|
||||
if (!match || !match[1]) return false; //No match
|
||||
|
||||
const start = cx.lineStart, children = [];
|
||||
|
||||
const quoteEnd = start + line.text.indexOf('[!');
|
||||
const typeStart = quoteEnd + 2;
|
||||
const typeEnd = typeStart + match[1].length;
|
||||
const bracketEnd = typeEnd + 1;
|
||||
|
||||
children.push(cx.elt('CalloutMarker', start, bracketEnd, [ cx.elt('CalloutMark', start, quoteEnd), cx.elt('CalloutType', typeStart, typeEnd) ]));
|
||||
|
||||
if(match[2]) children.push(cx.elt('CalloutTitle', bracketEnd + 1, start + line.text.length));
|
||||
|
||||
while (cx.nextLine() && line.text.startsWith('>'))
|
||||
{
|
||||
const pos = line.text.substring(1).search(/\S/) + 1;
|
||||
children.push(cx.elt('CalloutLine', cx.lineStart, cx.lineStart + line.text.length, [
|
||||
cx.elt('CalloutMark', cx.lineStart, cx.lineStart + pos),
|
||||
cx.elt('CalloutContent', cx.lineStart + pos, cx.lineStart + line.text.length),
|
||||
]))
|
||||
}
|
||||
|
||||
cx.addElement(cx.elt('CalloutBlock', start, cx.lineStart - 1, children));
|
||||
|
||||
return true;
|
||||
}
|
||||
}],
|
||||
props: [
|
||||
styleTags({
|
||||
'CalloutBlock': tags.special(tags.quote),
|
||||
'CalloutMarker': tags.meta,
|
||||
'CalloutMark': tags.meta,
|
||||
'CalloutType': tags.keyword,
|
||||
'CalloutTitle': tags.heading,
|
||||
'CalloutLine': tags.content,
|
||||
'CalloutContent': tags.content,
|
||||
})
|
||||
]
|
||||
};
|
||||
27
shared/grammar/tag.extension.ts
Normal file
27
shared/grammar/tag.extension.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { MarkdownConfig } from '@lezer/markdown';
|
||||
import { styleTags, Tag, tags } from '@lezer/highlight';
|
||||
|
||||
export const tagTag = Tag.define('tag');
|
||||
export const tag: MarkdownConfig = {
|
||||
defineNodes: [
|
||||
'Tag',
|
||||
'TagMeta',
|
||||
],
|
||||
parseInline: [{
|
||||
name: 'Tag',
|
||||
parse(cx, next, pos)
|
||||
{
|
||||
//35 == '#'
|
||||
if (cx.slice(pos, pos + 1).charCodeAt(0) !== 35 || String.fromCharCode(next).trim() === '') return -1;
|
||||
|
||||
const end = cx.slice(pos, cx.end).search(/\s/);
|
||||
return cx.addElement(cx.elt('Tag', pos, end === -1 ? cx.end : end, [ cx.elt('TagMeta', pos, pos + 1) ]));
|
||||
},
|
||||
}],
|
||||
props: [
|
||||
styleTags({
|
||||
'Tag': tagTag,
|
||||
'TagMeta': tags.meta,
|
||||
})
|
||||
]
|
||||
};
|
||||
72
shared/grammar/wikilink.extension.ts
Normal file
72
shared/grammar/wikilink.extension.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import type { Element, MarkdownConfig } from '@lezer/markdown';
|
||||
import { styleTags, tags } from '@lezer/highlight';
|
||||
|
||||
export const wikilink: MarkdownConfig = {
|
||||
defineNodes: [
|
||||
'Wikilink',
|
||||
'WikilinkMeta',
|
||||
'WikilinkHref',
|
||||
'WikilinkTitle',
|
||||
],
|
||||
parseInline: [{
|
||||
name: 'Wikilink',
|
||||
before: 'Link',
|
||||
parse(cx, next, pos)
|
||||
{
|
||||
// 91 == '['
|
||||
if (next !== 91 || cx.slice(pos, pos + 1).charCodeAt(0) !== 91) return -1;
|
||||
|
||||
const match = /!?\[\[([^\[\]\|\#]+)?(#+[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/.exec(cx.slice(pos, cx.end));
|
||||
if(!match) return -1;
|
||||
|
||||
const start = pos, children: Element[] = [], end = start + match[0].length;
|
||||
|
||||
children.push(cx.elt('WikilinkMeta', start, start + 2));
|
||||
|
||||
if(match[1] && !match[2] && !match[3]) //Link only
|
||||
{
|
||||
children.push(cx.elt('WikilinkTitle', start + 2, end - 2));
|
||||
}
|
||||
else if(!match[1] && match[2] && match[3]) //Hash and title
|
||||
{
|
||||
children.push(cx.elt('WikilinkHref', start + 2, start + 2 + match[2].length));
|
||||
children.push(cx.elt('WikilinkMeta', start + 2 + match[2].length, start + 2 + match[2].length + 1));
|
||||
children.push(cx.elt('WikilinkTitle', start + 2 + match[2].length + 1, start + 2 + match[2].length + match[3].length));
|
||||
}
|
||||
else if(!match[1] && !match[2] && match[3]) //Hash only
|
||||
{
|
||||
children.push(cx.elt('WikilinkTitle', start + 2, end - 2));
|
||||
}
|
||||
else if(match[1] && match[2] && !match[3]) //Link and hash
|
||||
{
|
||||
children.push(cx.elt('WikilinkHref', start + 2, start + 2 + match[1].length));
|
||||
children.push(cx.elt('WikilinkTitle', start + 2 + match[1].length, start + 2 + match[1].length + match[2].length));
|
||||
}
|
||||
else if(match[1] && !match[2] && match[3]) //Link and title
|
||||
{
|
||||
children.push(cx.elt('WikilinkHref', start + 2, start + 2 + match[1].length));
|
||||
children.push(cx.elt('WikilinkMeta', start + 2 + match[1].length, start + 2 + match[1].length + 1));
|
||||
children.push(cx.elt('WikilinkTitle', start + 2 + match[1].length + 1, start + 2 + match[1].length + match[3].length));
|
||||
}
|
||||
else if(match[1] && match[2] && match[3]) //Link, hash and title
|
||||
{
|
||||
//console.log(cx.slice(pos, end), '/', cx.slice(start + 2, start + 2 + match[1].length + match[2].length), '/', cx.slice(start + 2 + match[1].length + match[2].length, start + 2 + match[1].length + match[2].length + 1), cx.slice(start + 2 + match[1].length + match[2].length + 1, start + 2 + match[1].length + match[2].length + match[3].length))
|
||||
children.push(cx.elt('WikilinkHref', start + 2, start + 2 + match[1].length + match[2].length));
|
||||
children.push(cx.elt('WikilinkMeta', start + 2 + match[1].length + match[2].length, start + 2 + match[1].length + match[2].length + 1));
|
||||
children.push(cx.elt('WikilinkTitle', start + 2 + match[1].length + match[2].length + 1, start + 2 + match[1].length + match[2].length + match[3].length));
|
||||
}
|
||||
|
||||
children.push(cx.elt('WikilinkMeta', end - 2, end));
|
||||
|
||||
return cx.addElement(cx.elt('Wikilink', start, end, children));
|
||||
},
|
||||
}],
|
||||
props: [
|
||||
styleTags({
|
||||
'Wikilink': tags.special(tags.content),
|
||||
'WikilinkMeta': tags.meta,
|
||||
'WikilinkHref': tags.link,
|
||||
'WikilinkTitle': tags.special(tags.link),
|
||||
})
|
||||
]
|
||||
};
|
||||
Reference in New Issue
Block a user