mirror of
https://github.com/kotaindah55/extended-markdown-syntax.git
synced 2026-07-22 05:38:06 +00:00
refactor: separate the rules besed on the token level
This commit is contained in:
parent
70872ad5a6
commit
48fa008223
3 changed files with 89 additions and 50 deletions
11
src/shared-configs/BlockRules.ts
Normal file
11
src/shared-configs/BlockRules.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { Format } from "src/enums";
|
||||
import { BlockFormat, BlockFormatRule } from "src/types";
|
||||
|
||||
export const BlockRules: Record<BlockFormat, BlockFormatRule> = {
|
||||
[Format.FENCED_DIV]: {
|
||||
char: ":",
|
||||
length: 3,
|
||||
exactLen: false,
|
||||
class: "fenced-div"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
import { Format } from "src/enums";
|
||||
import { MainFormat } from "src/types";
|
||||
|
||||
export const FormatRules: Record<MainFormat, { char: string, length: number, exactLen: boolean, allowSpace: boolean, getEl: () => Element }> = {
|
||||
[Format.INSERTION]: {
|
||||
char: "+",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
getEl: () => document.createElement("ins")
|
||||
},
|
||||
[Format.SPOILER]: {
|
||||
char: "|",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
getEl: () => {
|
||||
let spoilerEl = document.createElement("span");
|
||||
spoilerEl.addClass("spoiler");
|
||||
spoilerEl.addEventListener("click", (evt) => {
|
||||
let spoilerEl = evt.currentTarget as Element,
|
||||
isHidden = !spoilerEl.hasClass("spoiler-revealed");
|
||||
spoilerEl.toggleClass("spoiler-revealed", isHidden);
|
||||
});
|
||||
return spoilerEl;
|
||||
}
|
||||
},
|
||||
[Format.SUPERSCRIPT]: {
|
||||
char: "^",
|
||||
length: 1,
|
||||
exactLen: true,
|
||||
allowSpace: false,
|
||||
getEl: () => document.createElement("sup")
|
||||
},
|
||||
[Format.SUBSCRIPT]: {
|
||||
char: "~",
|
||||
length: 1,
|
||||
exactLen: true,
|
||||
allowSpace: false,
|
||||
getEl: () => document.createElement("sub")
|
||||
},
|
||||
// unused
|
||||
[Format.HIGHLIGHT]: {
|
||||
char: "=",
|
||||
length: 2,
|
||||
exactLen: false,
|
||||
allowSpace: true,
|
||||
getEl: () => document.createElement("mark")
|
||||
}
|
||||
}
|
||||
78
src/shared-configs/InlineRules.ts
Normal file
78
src/shared-configs/InlineRules.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import { Format } from "src/enums";
|
||||
import { InlineFormatRule, InlineFormat } from "src/types";
|
||||
|
||||
export const InlineRules: Record<InlineFormat, InlineFormatRule> = {
|
||||
[Format.INSERTION]: {
|
||||
char: "+",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
mustBeClosed: true,
|
||||
class: "ins",
|
||||
getEl: () => document.createElement("ins"),
|
||||
builtin: false
|
||||
},
|
||||
[Format.SPOILER]: {
|
||||
char: "|",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
mustBeClosed: true,
|
||||
class: "spoiler",
|
||||
getEl: () => {
|
||||
let spoilerEl = document.createElement("span");
|
||||
spoilerEl.addClass("spoiler");
|
||||
spoilerEl.addEventListener("click", (evt) => {
|
||||
let spoilerEl = evt.currentTarget as Element,
|
||||
isHidden = !spoilerEl.hasClass("spoiler-revealed");
|
||||
spoilerEl.toggleClass("spoiler-revealed", isHidden);
|
||||
});
|
||||
return spoilerEl;
|
||||
},
|
||||
builtin: false
|
||||
},
|
||||
[Format.SUPERSCRIPT]: {
|
||||
char: "^",
|
||||
length: 1,
|
||||
exactLen: true,
|
||||
allowSpace: false,
|
||||
mustBeClosed: true,
|
||||
class: "sup",
|
||||
getEl: () => document.createElement("sup"),
|
||||
builtin: false
|
||||
},
|
||||
[Format.SUBSCRIPT]: {
|
||||
char: "~",
|
||||
length: 1,
|
||||
exactLen: true,
|
||||
allowSpace: false,
|
||||
mustBeClosed: true,
|
||||
class: "sub",
|
||||
getEl: () => document.createElement("sub"),
|
||||
builtin: false
|
||||
},
|
||||
[Format.HIGHLIGHT]: {
|
||||
char: "=",
|
||||
length: 2,
|
||||
exactLen: false,
|
||||
allowSpace: true,
|
||||
mustBeClosed: false,
|
||||
class: "custom-highlight",
|
||||
getEl: () => document.createElement("mark"),
|
||||
builtin: true
|
||||
},
|
||||
[Format.CUSTOM_SPAN]: {
|
||||
char: "!",
|
||||
length: 2,
|
||||
exactLen: true,
|
||||
allowSpace: true,
|
||||
mustBeClosed: true,
|
||||
class: "custom-span",
|
||||
getEl() {
|
||||
let el = document.createElement("span");
|
||||
el.classList.add(InlineRules[Format.CUSTOM_SPAN].class);
|
||||
return el;
|
||||
},
|
||||
builtin: false
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue