-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from moonlight-mod/markdown-library
Markdown library
- Loading branch information
Showing
7 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ExtensionWebpackModule, Patch } from "@moonlight-mod/types"; | ||
|
||
export const patches: Patch[] = [ | ||
{ | ||
find: "/^(¯\\\\_\\(ツ\\)_\\/¯)/.exec", | ||
replace: [ | ||
{ | ||
match: /={newline:(.+?)},(.{1,2})=\(0,/, | ||
replacement: (_, rules, RULES) => | ||
`=require("markdown_markdown")._addRules({newline:${rules}}),${RULES}=(0,` | ||
}, | ||
{ | ||
match: /(?<=var (.{1,2})={RULES:.+?})/, | ||
replacement: (_, rulesets) => | ||
`;require("markdown_markdown")._applyRulesetBlacklist(${rulesets});` | ||
} | ||
] | ||
}, | ||
{ | ||
find: "then you probably need to add it to this file so that the rich chat box understands it.", | ||
replace: [ | ||
{ | ||
match: /(.)={link:{(.+?)},(.)=new Set/, | ||
replacement: (_, rulesDef, rules, syntaxBefore) => | ||
`__slateRules,${rulesDef}=__slateRules=require("markdown_markdown")._addSlateRules({link:{${rules}}),${syntaxBefore}=new Set` | ||
}, | ||
{ | ||
match: | ||
/(originalMatch:.}=(.);)(.+?)case"emoticon":(return .+?;)(.+?)case"link":{(.+?)}default:/, | ||
replacement: ( | ||
_, | ||
start, | ||
rule, | ||
body, | ||
plaintextReturn, | ||
otherRules, | ||
inlineStyleBody | ||
) => | ||
`${start}if(${rule}.type.startsWith("__moonlight_")){if(__slateRules[${rule}.type].type=="inlineStyle"){${inlineStyleBody}}else{${plaintextReturn}}}${body}case"emoticon":${plaintextReturn}${otherRules}case"link":{${inlineStyleBody}}default:` | ||
} | ||
] | ||
}, | ||
{ | ||
find: '"Slate: Unknown decoration attribute: "', | ||
replace: { | ||
match: /=({strong:.+?});/, | ||
replacement: (_, rules) => | ||
`=require("markdown_markdown")._addSlateDecorators(${rules});` | ||
} | ||
} | ||
]; | ||
|
||
export const webpackModules: Record<string, ExtensionWebpackModule> = { | ||
markdown: {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"id": "markdown", | ||
"meta": { | ||
"name": "Markdown", | ||
"tagline": "A library for adding new markdown rules", | ||
"authors": ["Cynosphere"], | ||
"tags": ["library"] | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/core-extensions/src/markdown/webpackModules/markdown.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-disable no-console */ | ||
import { | ||
MarkdownRule, | ||
Ruleset, | ||
SlateRule | ||
} from "@moonlight-mod/types/coreExtensions/markdown"; | ||
|
||
export const rules: Record< | ||
string, | ||
(rules: Record<string, MarkdownRule>) => MarkdownRule | ||
> = {}; | ||
export const slateRules: Record< | ||
string, | ||
(rules: Record<string, SlateRule>) => SlateRule | ||
> = {}; | ||
export const slateDecorators: Record<string, string> = {}; | ||
export const ruleBlacklists: Record<Ruleset, Record<string, boolean>> = { | ||
RULES: {}, | ||
CHANNEL_TOPIC_RULES: {}, | ||
VOICE_CHANNEL_STATUS_RULES: {}, | ||
EMBED_TITLE_RULES: {}, | ||
INLINE_REPLY_RULES: {}, | ||
GUILD_VERIFICATION_FORM_RULES: {}, | ||
GUILD_EVENT_RULES: {}, | ||
PROFILE_BIO_RULES: {}, | ||
AUTO_MODERATION_SYSTEM_MESSAGE_RULES: {}, | ||
NATIVE_SEARCH_RESULT_LINK_RULES: {} | ||
}; | ||
|
||
export function addRule( | ||
name: string, | ||
markdown: (rules: Record<string, MarkdownRule>) => MarkdownRule, | ||
slate: (rules: Record<string, SlateRule>) => SlateRule, | ||
decorator?: string | ||
) { | ||
rules[name] = markdown; | ||
slateRules[name] = slate; | ||
if (decorator != null) slateDecorators[name] = decorator; | ||
} | ||
|
||
export function blacklistFromRuleset(ruleset: Ruleset, name: string) { | ||
if (ruleBlacklists[ruleset] == null) ruleBlacklists[ruleset] = {}; | ||
ruleBlacklists[ruleset][name] = true; | ||
} | ||
|
||
export function _addRules(originalRules: Record<string, MarkdownRule>) { | ||
for (const name in rules) { | ||
originalRules["__moonlight_" + name] = rules[name](originalRules); | ||
} | ||
|
||
return originalRules; | ||
} | ||
|
||
export function _addSlateRules(originalRules: Record<string, SlateRule>) { | ||
for (const name in slateRules) { | ||
originalRules["__moonlight_" + name] = slateRules[name](originalRules); | ||
} | ||
|
||
return originalRules; | ||
} | ||
|
||
export function _addSlateDecorators(originalRules: Record<string, string>) { | ||
for (const name in slateDecorators) { | ||
originalRules["__moonlight_" + name] = slateDecorators[name]; | ||
} | ||
|
||
return originalRules; | ||
} | ||
|
||
export function _applyRulesetBlacklist( | ||
rulesets: Record<Ruleset, Record<string, MarkdownRule>> | ||
) { | ||
for (const ruleset of Object.keys(rulesets) as Ruleset[]) { | ||
if (ruleset === "RULES") continue; | ||
|
||
const rules = rulesets[ruleset]; | ||
for (const rule in ruleBlacklists[ruleset] || {}) { | ||
delete rules["__moonlight_" + rule]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// {{{ simple-markdown | ||
|
||
export type SingleASTNode = { | ||
type: string; | ||
[key: string]: any; | ||
}; | ||
|
||
export type UntypedASTNode = { | ||
[key: string]: any; | ||
}; | ||
|
||
export type ASTNode = SingleASTNode | Array<SingleASTNode>; | ||
|
||
export type Parser = ( | ||
source: string, | ||
state?: State | null | undefined | ||
) => Array<SingleASTNode>; | ||
|
||
export type ParseFunction = ( | ||
capture: Capture, | ||
nestedParse: Parser, | ||
state: State | ||
) => UntypedASTNode | ASTNode; | ||
|
||
export type Capture = | ||
| (Array<string> & { | ||
index: number; | ||
}) | ||
| (Array<string> & { | ||
index?: number; | ||
}); | ||
|
||
export type State = { | ||
key?: string | number | undefined; | ||
inline?: boolean | null | undefined; | ||
[key: string]: any; | ||
}; | ||
|
||
export type MatchFunction = { | ||
regex?: RegExp; | ||
} & (( | ||
source: string, | ||
state: State, | ||
prevCapture: string | ||
) => Capture | null | undefined); | ||
|
||
export type Output<Result> = ( | ||
node: ASTNode, | ||
state?: State | null | undefined | ||
) => Result; | ||
|
||
export type SingleNodeOutput<Result> = ( | ||
node: SingleASTNode, | ||
nestedOutput: Output<Result>, | ||
state: State | ||
) => Result; | ||
|
||
// }}} | ||
|
||
export type ValidFlags = "g" | "i" | "m" | "s" | "u" | "y" | undefined; | ||
|
||
export type MarkdownRule = { | ||
order: number; | ||
match: MatchFunction; | ||
parse: ParseFunction; | ||
react: SingleNodeOutput<React.ReactNode>; | ||
}; | ||
|
||
export type SlateRule = | ||
| { | ||
type: "skip"; | ||
} | ||
| { | ||
type: "verbatim"; | ||
} | ||
| { | ||
type: "inlineObject"; | ||
} | ||
| { | ||
type: "inlineStyle"; | ||
before: string; | ||
after: string; | ||
}; | ||
|
||
export type Ruleset = | ||
| "RULES" | ||
| "CHANNEL_TOPIC_RULES" | ||
| "VOICE_CHANNEL_STATUS_RULES" | ||
| "EMBED_TITLE_RULES" | ||
| "INLINE_REPLY_RULES" | ||
| "GUILD_VERIFICATION_FORM_RULES" | ||
| "GUILD_EVENT_RULES" | ||
| "PROFILE_BIO_RULES" | ||
| "AUTO_MODERATION_SYSTEM_MESSAGE_RULES" | ||
| "NATIVE_SEARCH_RESULT_LINK_RULES"; | ||
|
||
export type Markdown = { | ||
rules: Record<string, MarkdownRule>; | ||
slateRules: Record<string, SlateRule>; | ||
slateDecorators: Record<string, string>; | ||
ruleBlacklists: Record<Ruleset, Record<string, boolean>>; | ||
|
||
addRule: ( | ||
name: string, | ||
markdown: (rules: Record<string, MarkdownRule>) => MarkdownRule, | ||
slate: (rules: Record<string, SlateRule>) => SlateRule, | ||
decorator?: string | undefined | ||
) => void; | ||
blacklistFromRuleset: (ruleset: Ruleset, name: string) => void; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters