this repo has no description

Finish markdown library (I think)

Changed files
+243 -99
packages
core-extensions
src
markdown
webpackModules
types
+54 -2
packages/core-extensions/src/markdown/index.ts
···
-
import { Patch } from "@moonlight-mod/types";
+
import { ExtensionWebpackModule, Patch } from "@moonlight-mod/types";
-
export const patches: Patch[] = [];
+
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: {}
+
};
-94
packages/core-extensions/src/markdown/types.ts
···
-
// {{{ simple-markdown
-
-
type SingleASTNode = {
-
type: string;
-
[key: string]: any;
-
};
-
-
type UnTypedASTNode = {
-
[key: string]: any;
-
};
-
-
type ASTNode = SingleASTNode | Array<SingleASTNode>;
-
-
type Parser = (
-
source: string,
-
state?: State | null | undefined
-
) => Array<SingleASTNode>;
-
-
type ParseFunction = (
-
capture: Capture,
-
nestedParse: Parser,
-
state: State
-
) => UnTypedASTNode | ASTNode;
-
-
type Capture =
-
| (Array<string> & {
-
index: number;
-
})
-
| (Array<string> & {
-
index?: number;
-
});
-
-
type State = {
-
key?: string | number | undefined;
-
inline?: boolean | null | undefined;
-
[key: string]: any;
-
};
-
-
type MatchFunction = {
-
regex?: RegExp;
-
} & ((
-
source: string,
-
state: State,
-
prevCapture: string
-
) => Capture | null | undefined);
-
-
type Output<Result> = (
-
node: ASTNode,
-
state?: State | null | undefined
-
) => Result;
-
-
type ArrayNodeOutput<Result> = (
-
node: Array<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: ArrayNodeOutput<React.ReactNode>;
-
};
-
-
export type SlateRule =
-
| {
-
type: "skip";
-
}
-
| {
-
type: "verbatim";
-
}
-
| {
-
type: "inlineObject";
-
}
-
| {
-
type: "inlineStyle";
-
before: string;
-
after: string;
-
};
-
-
export type Ruleset =
-
| "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";
+68 -3
packages/core-extensions/src/markdown/webpackModules/markdown.ts
···
-
import { MarkdownRule, Ruleset, SlateRule } from "../types";
+
/* eslint-disable no-console */
+
import {
+
MarkdownRule,
+
Ruleset,
+
SlateRule
+
} from "@moonlight-mod/types/coreExtensions/markdown";
-
export const rules: Record<string, MarkdownRule> = {};
-
export const slateRules: Record<string, SlateRule> = {};
+
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: {},
···
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];
+
}
+
}
+
}
+2
packages/types/src/coreExtensions.ts
···
export type CommonFlux = FluxDefault;
export type CommonComponents = CommonComponents_; // lol
export type CommonFluxDispatcher = Dispatcher<any>;
+
+
export * as Markdown from "./coreExtensions/markdown";
+110
packages/types/src/coreExtensions/markdown.ts
···
+
// {{{ 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;
+
};
+3
packages/types/src/discord/require.ts
···
CommonComponents,
CommonFluxDispatcher
} from "../coreExtensions";
+
import { Markdown } from "../coreExtensions/markdown";
declare function WebpackRequire(id: string): any;
declare function WebpackRequire(id: "spacepack_spacepack"): {
···
Settings: Settings;
default: Settings;
};
+
+
declare function WebpackRequire(id: "markdown_markdown"): Markdown;
export default WebpackRequire;
+6
packages/types/src/import.d.ts
···
export const Settings: CoreExtensions.Settings;
export default Settings;
}
+
+
declare module "@moonlight-mod/wp/markdown_markdown" {
+
import { CoreExtensions } from "@moonlight-mod/types";
+
const Markdown: CoreExtensions.Markdown.Markdown;
+
export = Markdown;
+
}