From ffae0459186c4184aad83a97401bbd9346ef746f Mon Sep 17 00:00:00 2001
From: Pho Thin \[\.([a-z0-9A-Z\s]+)\]<\/p>[\n]?<(.+)>/g,
- `<$2 class="$1">`
- )
+ return [
+ {
+ type: "output",
+ filter: (text) => {
+ return (
+ text
+ // Add class for list (ol, ul)
+ .replace(
+ / \[\.([a-z0-9A-Z\s]+)\]<\/p>[\n]?<(.+)>/g,
+ `<$2 class="$1">`,
+ )
- // Add class for other blocks
- .replace(/<(.+)>\[\.([a-z0-9A-Z\s]+)\]/g, `<$1 class="$2">`)
+ // Add class for other blocks
+ .replace(/<(.+)>\[\.([a-z0-9A-Z\s]+)\]/g, `<$1 class="$2">`)
- // Prevent class name with 2 dashs being replace by `` tag
- .replace(/class="(.+)"/g, function (str) {
- if (str.indexOf("") !== -1) {
- return str.replace(/<[/]?em>/g, "_");
- }
- return str;
- })
- );
- },
- },
- ];
+ // Prevent class name with 2 dashs being replace by `` tag
+ .replace(/class="(.+)"/g, (str) => {
+ if (str.indexOf("") !== -1) {
+ return str.replace(/<[/]?em>/g, "_");
+ }
+ return str;
+ })
+ );
+ },
+ },
+ ];
}
registerExtension("customClass", customClass());
diff --git a/src/extensions/icons.ts b/src/extensions/icons.ts
index 6578e05..0181110 100644
--- a/src/extensions/icons.ts
+++ b/src/extensions/icons.ts
@@ -1,6 +1,6 @@
import {
- registerExtension,
- type MmExtension,
+ type MmExtension,
+ registerExtension,
} from "../manage-extensions/index.js";
/**
@@ -17,28 +17,27 @@ import {
* ```
*/
function icons(): MmExtension[] {
- return [
- {
- type: "lang",
- regex: "\\B(\\\\)?@fa-([\\S]+)\\b",
- replace: function (a: any, b: string, c: string) {
- return b === "\\" ? a : '' + "";
- },
- },
- {
- type: "output",
- filter: (text: string) => {
- const scriptTag = `
+ return [
+ {
+ type: "lang",
+ regex: "\\B(\\\\)?@fa-([\\S]+)\\b",
+ replace: (a: any, b: string, c: string) =>
+ b === "\\" ? a : ``,
+ },
+ {
+ type: "output",
+ filter: (text: string) => {
+ const scriptTag = `
`;
- return scriptTag + text;
- },
- },
- ];
+ return scriptTag + text;
+ },
+ },
+ ];
}
registerExtension("icons", icons());
diff --git a/src/extensions/index.ts b/src/extensions/index.ts
index e659b06..fd17c6d 100644
--- a/src/extensions/index.ts
+++ b/src/extensions/index.ts
@@ -7,11 +7,11 @@ import twitter from "./twitter.js";
import youtube from "./youtube.js";
export {
- showdownMathjax,
- showdownprism,
- copyCode,
- customClass,
- icons,
- twitter,
- youtube,
+ showdownMathjax,
+ showdownprism,
+ copyCode,
+ customClass,
+ icons,
+ twitter,
+ youtube,
};
diff --git a/src/extensions/twitter.ts b/src/extensions/twitter.ts
index 817f53a..6f233b9 100644
--- a/src/extensions/twitter.ts
+++ b/src/extensions/twitter.ts
@@ -1,6 +1,6 @@
import {
- registerExtension,
- type MmExtension,
+ type MmExtension,
+ registerExtension,
} from "../manage-extensions/index.js";
/**
* Twitter Extension
@@ -10,52 +10,38 @@ import {
* #hashtag -> #hashtag
*/
function twitter(): MmExtension[] {
- return [
- // @username syntax
- {
- type: "lang",
- regex: "\\B(\\\\)?@([\\S]+)\\b",
- replace: function (match: any, leadingSlash: string, username: string) {
- // Check if we matched the leading \ and return nothing changed if so
- if (leadingSlash === "\\") {
- return match;
- } else {
- return (
- '@' +
- username +
- ""
- );
- }
- },
- },
- // #hashtag syntax
- {
- type: "lang",
- regex: "\\B(\\\\)?#([\\S]+)\\b",
- replace: function (match: any, leadingSlash: string, tag: string) {
- // Check if we matched the leading \ and return nothing changed if so
- if (leadingSlash === "\\") {
- return match;
- } else {
- return (
- '#' +
- tag +
- ""
- );
- }
- },
- },
- // Escaped @'s
- {
- type: "lang",
- regex: "\\\\@",
- replace: "@",
- },
- ];
+ return [
+ // @username syntax
+ {
+ type: "lang",
+ regex: "\\B(\\\\)?@([\\S]+)\\b",
+ replace: (match: any, leadingSlash: string, username: string) => {
+ // Check if we matched the leading \ and return nothing changed if so
+ if (leadingSlash === "\\") {
+ return match;
+ }
+ return `@${username}`;
+ },
+ },
+ // #hashtag syntax
+ {
+ type: "lang",
+ regex: "\\B(\\\\)?#([\\S]+)\\b",
+ replace: (match: any, leadingSlash: string, tag: string) => {
+ // Check if we matched the leading \ and return nothing changed if so
+ if (leadingSlash === "\\") {
+ return match;
+ }
+ return `#${tag}`;
+ },
+ },
+ // Escaped @'s
+ {
+ type: "lang",
+ regex: "\\\\@",
+ replace: "@",
+ },
+ ];
}
registerExtension("twitter", twitter());
diff --git a/src/extensions/youtube.ts b/src/extensions/youtube.ts
index 11764f9..2602771 100644
--- a/src/extensions/youtube.ts
+++ b/src/extensions/youtube.ts
@@ -1,6 +1,6 @@
import {
- registerExtension,
- type MmExtension,
+ type MmExtension,
+ registerExtension,
} from "../manage-extensions/index.js";
/**
@@ -20,16 +20,16 @@ import {
*
*/
function youtube(): MmExtension[] {
- return [
- {
- type: "lang",
- regex: /{% youtube (.+?) %}/g,
- replace: (_match: any, url: string) => {
- const videoId = url.split("v=")[1] || url.split("/").pop();
- return ``;
- },
- },
- ];
+ return [
+ {
+ type: "lang",
+ regex: /{% youtube (.+?) %}/g,
+ replace: (_match: any, url: string) => {
+ const videoId = url.split("v=")[1] || url.split("/").pop();
+ return ``;
+ },
+ },
+ ];
}
registerExtension("youtube", youtube());
diff --git a/src/frontmatter/index.ts b/src/frontmatter/index.ts
index 2ac5fc9..3f8a2b1 100644
--- a/src/frontmatter/index.ts
+++ b/src/frontmatter/index.ts
@@ -1,18 +1,18 @@
import { load } from "js-yaml";
type DataProps = {
- lines: string[];
- metaIndices: number[];
+ lines: string[];
+ metaIndices: number[];
};
type FrontMatterResult \ <div>foo</div> ... ... hello there @tivie hello there hello there @tivie some text withunderscoresin middle some text with__underscores__in middle #header a line
- * wrapped in two a line underlined word underlined word \Copy$&`
- );
- },
- },
- ];
+ return text.replace(
+ /
Copy$&`,
+ );
+ },
+ },
+ ];
}
registerExtension("copyCode", copyCode());
diff --git a/src/extensions/custom_class.ts b/src/extensions/custom_class.ts
index 31cea3b..33ec544 100644
--- a/src/extensions/custom_class.ts
+++ b/src/extensions/custom_class.ts
@@ -1,6 +1,6 @@
import {
- registerExtension,
- type MmExtension,
+ type MmExtension,
+ registerExtension,
} from "../manage-extensions/index.js";
/**
@@ -43,32 +43,32 @@ import {
* @returns {MmExtension[]}
*/
function customClass(): MmExtension[] {
- return [
- {
- type: "output",
- filter: (text) => {
- return (
- text
- // Add class for list (ol, ul)
- .replace(
- /
- *
- *
- *
- * ```
- *
- * **disableForced4SpacesIndentedSublists** = true
- *
- * ```html
- *
- *
- *
- *
- *
- *
- *
- *
- * ```
- * @default false
- */
- disableForced4SpacesIndentedSublists?: MmmarkConverterOptions["disableForced4SpacesIndentedSublists"];
- /**
- * Add showdown extensions to the new converter can be showdown extensions or "global" extensions name.
- *
- * @default
- * []
- *
- * Add extensions to the new converter can be showdown extensions or "global" extensions name.
- */
- extensions?: MmmarkConverterOptions["extensions"];
- /**
- * Generate header ids compatible with github style (spaces are replaced
- * with dashes and a bunch of non alphanumeric chars are removed).
- *
- * @example
- * **input**:
- *
- * ```md
- * # This is a header with @#$%
- * ```
- *
- * **ghCompatibleHeaderId** = false
- *
- * ```html
- *
- *
- * This is a header
- * ```
- *
- * **ghCompatibleHeaderId** = true
- *
- * ```html
- * This is a header with @#$%
- * ```
- * @default false
- */
- ghCompatibleHeaderId?: MmmarkConverterOptions["ghCompatibleHeaderId"];
- /**
- * Enables support for github @mentions, which links to the github profile page of the username mentioned.
- *
- * @example
- * **input**:
- *
- * ```md
- * hello there @tivie
- * ```
- *
- * **ghMentions** = false
- *
- * ```html
- * This is a header
- * ```
- *
- * **noHeaderId** = true
- *
- * ```html
- * This is a header
- * ```
- * @default false
- */
- noHeaderId?: MmmarkConverterOptions["noHeaderId"];
- /**
- * Omit the trailing newline in a code block.
- * By default, showdown adds a newline before the closing tags in code blocks. By enabling this option, that newline is removed.
- * This option affects both indented and fenced (gfm style) code blocks.
- *
- * @example
- *
- * **input**:
- *
- * ```md
- * var foo = 'bar';
- * ```
- *
- * **omitExtraWLInCodeBlocks** = false:
- *
- * ```html
- *
- * ```
- * **omitExtraWLInCodeBlocks** = true:
- *
- * ```html
- * var foo = 'bar';
- *
- * ```
- * @default false
- */
- omitExtraWLInCodeBlocks?: MmmarkConverterOptions["omitExtraWLInCodeBlocks"];
- /**
- * Add a prefix to the generated header ids.
- * Passing a string will prefix that string to the header id.
- * Setting to true will add a generic 'section' prefix.
- *
- * @default false
- */
- prefixHeaderId?: MmmarkConverterOptions["prefixHeaderId"];
- /**
- * Remove only spaces, ' and " from generated header ids (including prefixes),
- * replacing them with dashes (-).
- * WARNING: This might result in malformed ids.
- *
- * @default false
- */
- rawHeaderId?: MmmarkConverterOptions["rawHeaderId"];
- /**
- * Setting this option to true will prevent showdown from modifying the prefix.
- * This might result in malformed IDs (if, for instance, the " char is used in the prefix).
- * Has no effect if prefixHeaderId is set to false.
- *
- * @default false
- */
- rawPrefixHeaderId?: MmmarkConverterOptions["rawPrefixHeaderId"];
- /**
- * Makes adding a space between # and the header text mandatory.
- *
- * @example
- * **input**:
- *
- * ```md
- * #header
- * ```
- *
- * **requireSpaceBeforeHeadingText** = false
- *
- * ```html
- * var foo = 'bar';
header
- * ```
- *
- * **simpleLineBreaks** = true
- *
- * ```html
- *
- * wrapped in two
<div>foo</div>
+ * ``` + * @default false + */ + backslashEscapesHTMLTags?: MmmarkConverterOptions["backslashEscapesHTMLTags"]; + /** + * Use text in curly braces as header id. + * + * @example + * ```md + * ## Sample header {real-id} will use real-id as id + * ``` + * @default false + */ + customizedHeaderId?: MmmarkConverterOptions["customizedHeaderId"]; + /** + * Disables the requirement of indenting sublists by 4 spaces for them to be nested, + * effectively reverting to the old behavior where 2 or 3 spaces were enough. + * + * @example + * **input**: + * + * ```md + * - one + * - two + * + * ... + * + * - one + * - two + * ``` + * + * **disableForced4SpacesIndentedSublists** = false + * + * ```html + *...
+ *...
+ *hello there @tivie
+ * ``` + * + * **ghMentions** = true + * + * ```html + *hello there hello there @tivie
+ * ``` + * @default https://github.com/{u} + */ + ghMentionsLink?: MmmarkConverterOptions["ghMentionsLink"]; + /** + * Turning this on will stop showdown from interpreting underscores in the middle of + * words as and and instead treat them as literal underscores. + * + * @example + * **input**: + * + * ```md + * some text with__underscores__in middle + * ``` + * + * **literalMidWordUnderscores** = false + * + * ```html + *some text withunderscoresin middle
+ * ``` + * + * **literalMidWordUnderscores** = true + * + * ```html + *some text with__underscores__in middle
+ * ``` + * @default false + */ + literalMidWordUnderscores?: MmmarkConverterOptions["literalMidWordUnderscores"]; + /** + * Disable the automatic generation of header ids. + * Showdown generates an id for headings automatically. This is useful for linking to a specific header. + * This behavior, however, can be disabled with this option. + * + * @example + * **input**: + * + * ```md + * # This is a header + * ``` + * + * **noHeaderId** = false + * + * ```html + *var foo = 'bar';
+ *
+ * ```
+ * **omitExtraWLInCodeBlocks** = true:
+ *
+ * ```html
+ * var foo = 'bar';
+ * ```
+ * @default false
+ */
+ omitExtraWLInCodeBlocks?: MmmarkConverterOptions["omitExtraWLInCodeBlocks"];
+ /**
+ * Add a prefix to the generated header ids.
+ * Passing a string will prefix that string to the header id.
+ * Setting to true will add a generic 'section' prefix.
+ *
+ * @default false
+ */
+ prefixHeaderId?: MmmarkConverterOptions["prefixHeaderId"];
+ /**
+ * Remove only spaces, ' and " from generated header ids (including prefixes),
+ * replacing them with dashes (-).
+ * WARNING: This might result in malformed ids.
+ *
+ * @default false
+ */
+ rawHeaderId?: MmmarkConverterOptions["rawHeaderId"];
+ /**
+ * Setting this option to true will prevent showdown from modifying the prefix.
+ * This might result in malformed IDs (if, for instance, the " char is used in the prefix).
+ * Has no effect if prefixHeaderId is set to false.
+ *
+ * @default false
+ */
+ rawPrefixHeaderId?: MmmarkConverterOptions["rawPrefixHeaderId"];
+ /**
+ * Makes adding a space between # and the header text mandatory.
+ *
+ * @example
+ * **input**:
+ *
+ * ```md
+ * #header
+ * ```
+ *
+ * **requireSpaceBeforeHeadingText** = false
+ *
+ * ```html
+ * #header
+ * ``` + * + * @default false + */ + requireSpaceBeforeHeadingText?: MmmarkConverterOptions["requireSpaceBeforeHeadingText"]; + /** + * Parses line breaks as like GitHub does, without needing 2 spaces at the end of the line. + * + * @example + * **input**: + * + * ```md + * a line + * wrapped in two + * ``` + * + * **simpleLineBreaks** = false + * + * ```html + *a line + * wrapped in two
+ * ``` + * + * **simpleLineBreaks** = true + * + * ```html + *a line
+ * wrapped in two
underlined word
+ * ``` + * + * **underline** = true + * ```html + *underlined word
+ * ``` + * @default false + */ + underline?: MmmarkConverterOptions["underline"]; }; /** @@ -391,51 +391,51 @@ type MmmarkUserSelectOptions = { * @returns The options object for the Markdown converter. */ const getAllOptions = ( - options?: MmmarkUserSelectOptions + options?: MmmarkUserSelectOptions, ): MmmarkConverterOptions => { - const dfoptions: MmmarkConverterOptions = { - emoji: true, - ghCodeBlocks: true, - parseImgDimensions: true, - simplifiedAutoLink: true, - tables: true, - tasklists: true, - completeHTMLDocument: false, - headerLevelStart: 1, - ellipsis: true, - encodeEmails: true, - metadata: false, - openLinksInNewWindow: true, - strikethrough: true, - // --------------------------------------------------------------------------------- - backslashEscapesHTMLTags: options?.backslashEscapesHTMLTags ?? false, - customizedHeaderId: options?.customizedHeaderId ?? false, - disableForced4SpacesIndentedSublists: - options?.disableForced4SpacesIndentedSublists ?? false, - extensions: options?.extensions ?? [], - ghCompatibleHeaderId: options?.ghCompatibleHeaderId ?? false, - ghMentions: options?.ghMentions ?? false, - ghMentionsLink: options?.ghMentionsLink ?? "https://github.com/{u}", - literalMidWordUnderscores: options?.literalMidWordUnderscores ?? false, - noHeaderId: options?.noHeaderId ?? false, - omitExtraWLInCodeBlocks: options?.omitExtraWLInCodeBlocks ?? false, - prefixHeaderId: options?.prefixHeaderId ?? false, - rawHeaderId: options?.rawHeaderId ?? false, - rawPrefixHeaderId: options?.rawPrefixHeaderId ?? false, - requireSpaceBeforeHeadingText: - options?.requireSpaceBeforeHeadingText ?? false, - simpleLineBreaks: options?.simpleLineBreaks ?? false, - splitAdjacentBlockquotes: options?.splitAdjacentBlockquotes ?? false, - smartIndentationFix: options?.smartIndentationFix ?? false, - smoothLivePreview: options?.smoothLivePreview ?? false, - tablesHeaderId: options?.tablesHeaderId ?? false, - underline: options?.underline ?? false, - }; - return dfoptions; + const dfoptions: MmmarkConverterOptions = { + emoji: true, + ghCodeBlocks: true, + parseImgDimensions: true, + simplifiedAutoLink: true, + tables: true, + tasklists: true, + completeHTMLDocument: false, + headerLevelStart: 1, + ellipsis: true, + encodeEmails: true, + metadata: false, + openLinksInNewWindow: true, + strikethrough: true, + // --------------------------------------------------------------------------------- + backslashEscapesHTMLTags: options?.backslashEscapesHTMLTags ?? false, + customizedHeaderId: options?.customizedHeaderId ?? false, + disableForced4SpacesIndentedSublists: + options?.disableForced4SpacesIndentedSublists ?? false, + extensions: options?.extensions ?? [], + ghCompatibleHeaderId: options?.ghCompatibleHeaderId ?? false, + ghMentions: options?.ghMentions ?? false, + ghMentionsLink: options?.ghMentionsLink ?? "https://github.com/{u}", + literalMidWordUnderscores: options?.literalMidWordUnderscores ?? false, + noHeaderId: options?.noHeaderId ?? false, + omitExtraWLInCodeBlocks: options?.omitExtraWLInCodeBlocks ?? false, + prefixHeaderId: options?.prefixHeaderId ?? false, + rawHeaderId: options?.rawHeaderId ?? false, + rawPrefixHeaderId: options?.rawPrefixHeaderId ?? false, + requireSpaceBeforeHeadingText: + options?.requireSpaceBeforeHeadingText ?? false, + simpleLineBreaks: options?.simpleLineBreaks ?? false, + splitAdjacentBlockquotes: options?.splitAdjacentBlockquotes ?? false, + smartIndentationFix: options?.smartIndentationFix ?? false, + smoothLivePreview: options?.smoothLivePreview ?? false, + tablesHeaderId: options?.tablesHeaderId ?? false, + underline: options?.underline ?? false, + }; + return dfoptions; }; export { - getAllOptions, - type MmmarkConverterOptions, - type MmmarkUserSelectOptions, + getAllOptions, + type MmmarkConverterOptions, + type MmmarkUserSelectOptions, }; diff --git a/src/manage-extensions/index.ts b/src/manage-extensions/index.ts index aa4a1b6..4a63ae8 100644 --- a/src/manage-extensions/index.ts +++ b/src/manage-extensions/index.ts @@ -10,10 +10,10 @@ type MmExtension = ShowdownExtension; * @throws Throws if `name` is not of type string. */ function registerExtension( - name: string, - ext: MmExtension | MmExtension[] | (() => MmExtension[] | MmExtension) + name: string, + ext: MmExtension | MmExtension[] | (() => MmExtension[] | MmExtension), ): void { - Showdown.extension(name, ext); + Showdown.extension(name, ext); } /** @@ -23,7 +23,7 @@ function registerExtension( * @returns Returns `true` if the extension is valid showdown extension, otherwise `false`. */ function validateExtension(ext: MmExtension[] | MmExtension): boolean { - return Showdown.validateExtension(ext); + return Showdown.validateExtension(ext); } /** @@ -32,12 +32,12 @@ function validateExtension(ext: MmExtension[] | MmExtension): boolean { * @returns {void} */ function removeExtensions(): void { - Showdown.resetExtensions(); + Showdown.resetExtensions(); } export { - registerExtension, - validateExtension, - removeExtensions, - type MmExtension, + registerExtension, + validateExtension, + removeExtensions, + type MmExtension, }; diff --git a/src/subparser/index.ts b/src/subparser/index.ts index 43e95d2..07a5d7e 100644 --- a/src/subparser/index.ts +++ b/src/subparser/index.ts @@ -10,7 +10,7 @@ type MmSubParser = SubParser; * @throws Throws if `name` is not of type string. */ function registerSubParser(name: string, func: SubParser): void { - return Showdown.subParser(name, func); + return Showdown.subParser(name, func); } export { registerSubParser, type MmSubParser };