Skip to content

Commit

Permalink
modified: src/converter/index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
phothinmg committed Aug 22, 2024
1 parent b457e2c commit ffae045
Show file tree
Hide file tree
Showing 11 changed files with 603 additions and 618 deletions.
10 changes: 5 additions & 5 deletions src/converter/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Showdown, { type Converter } from "showdown";

import {
type MmmarkConverterOptions,
type MmmarkUserSelectOptions,
getAllOptions,
type MmmarkConverterOptions,
type MmmarkUserSelectOptions,
getAllOptions,
} from "../helper/getalloptions.js";

type MmConverter = Converter;

function mdConverter(options?: MmmarkUserSelectOptions) {
const opts: MmmarkConverterOptions = getAllOptions(options);
return new Showdown.Converter(opts);
const opts: MmmarkConverterOptions = getAllOptions(options);
return new Showdown.Converter(opts);
}

export { mdConverter, type MmConverter };
62 changes: 31 additions & 31 deletions src/extensions/copy_code.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import {
registerExtension,
type MmExtension,
type MmExtension,
registerExtension,
} from "../manage-extensions/index.js";

/**
*
*/
declare global {
interface Window {
copyCodeListener?: boolean;
}
interface Window {
copyCodeListener?: boolean;
}
}

/**
* Adds an event listener to the window that listens for clicks.
* @param {String} className The class name of the button.
*/
function addListener(className: string) {
if (typeof window !== "undefined" && window.copyCodeListener !== true) {
window.addEventListener("click", (e) => {
const target = e.target as HTMLElement;
if (
target?.classList?.contains(className) &&
target.nextElementSibling?.tagName === "PRE"
) {
navigator.clipboard.writeText(
(target.nextElementSibling as HTMLPreElement).innerText
);
}
});
window.copyCodeListener = true;
}
if (typeof window !== "undefined" && window.copyCodeListener !== true) {
window.addEventListener("click", (e) => {
const target = e.target as HTMLElement;
if (
target?.classList?.contains(className) &&
target.nextElementSibling?.tagName === "PRE"
) {
navigator.clipboard.writeText(
(target.nextElementSibling as HTMLPreElement).innerText,
);
}
});
window.copyCodeListener = true;
}
}
/**
* showdownCopyCode
Expand All @@ -40,19 +40,19 @@ function addListener(className: string) {
* @function
*/
function copyCode({ className = "copy-code" } = {}): MmExtension[] {
return [
{
type: "output",
filter: function (text: string) {
addListener(className);
return [
{
type: "output",
filter: (text: string) => {
addListener(className);

return text.replace(
/<pre.*><code/g,
`<button class="${className}">Copy</button>$&`
);
},
},
];
return text.replace(
/<pre.*><code/g,
`<button class="${className}">Copy</button>$&`,
);
},
},
];
}

registerExtension("copyCode", copyCode());
Expand Down
52 changes: 26 additions & 26 deletions src/extensions/custom_class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
registerExtension,
type MmExtension,
type MmExtension,
registerExtension,
} from "../manage-extensions/index.js";

/**
Expand Down Expand Up @@ -43,32 +43,32 @@ import {
* @returns {MmExtension[]}
*/
function customClass(): MmExtension[] {
return [
{
type: "output",
filter: (text) => {
return (
text
// Add class for list (ol, ul)
.replace(
/<p>\[\.([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(
/<p>\[\.([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 `<em>` tag
.replace(/class="(.+)"/g, function (str) {
if (str.indexOf("<em>") !== -1) {
return str.replace(/<[/]?em>/g, "_");
}
return str;
})
);
},
},
];
// Prevent class name with 2 dashs being replace by `<em>` tag
.replace(/class="(.+)"/g, (str) => {
if (str.indexOf("<em>") !== -1) {
return str.replace(/<[/]?em>/g, "_");
}
return str;
})
);
},
},
];
}
registerExtension("customClass", customClass());

Expand Down
35 changes: 17 additions & 18 deletions src/extensions/icons.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
registerExtension,
type MmExtension,
type MmExtension,
registerExtension,
} from "../manage-extensions/index.js";

/**
Expand All @@ -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 : '<i class="fa fa-' + c + '">' + "</i>";
},
},
{
type: "output",
filter: (text: string) => {
const scriptTag = `
return [
{
type: "lang",
regex: "\\B(\\\\)?@fa-([\\S]+)\\b",
replace: (a: any, b: string, c: string) =>
b === "\\" ? a : `<i class="fa fa-${c}"></i>`,
},
{
type: "output",
filter: (text: string) => {
const scriptTag = `
<script>
var script = document.createElement("script");
script.src = "https://kit.fontawesome.com/50c925d5df.js";
script.crossorigin = "anonymous";
document.head.appendChild(script);
</script>`;
return scriptTag + text;
},
},
];
return scriptTag + text;
},
},
];
}

registerExtension("icons", icons());
Expand Down
14 changes: 7 additions & 7 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
82 changes: 34 additions & 48 deletions src/extensions/twitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
registerExtension,
type MmExtension,
type MmExtension,
registerExtension,
} from "../manage-extensions/index.js";
/**
* Twitter Extension
Expand All @@ -10,52 +10,38 @@ import {
* #hashtag -> <a href="http://twitter.com/search/%23hashtag">#hashtag</a>
*/
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 (
'<a href="http://twitter.com/' +
username +
'">@' +
username +
"</a>"
);
}
},
},
// #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 (
'<a href="http://twitter.com/search/%23' +
tag +
'">#' +
tag +
"</a>"
);
}
},
},
// 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 `<a href="http://twitter.com/${username}">@${username}</a>`;
},
},
// #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 `<a href="http://twitter.com/search/%23${tag}">#${tag}</a>`;
},
},
// Escaped @'s
{
type: "lang",
regex: "\\\\@",
replace: "@",
},
];
}
registerExtension("twitter", twitter());

Expand Down
24 changes: 12 additions & 12 deletions src/extensions/youtube.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
registerExtension,
type MmExtension,
type MmExtension,
registerExtension,
} from "../manage-extensions/index.js";

/**
Expand All @@ -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 `<iframe width="560" height="315" src="https://www.youtube.com/embed/${videoId}" frameborder="0" allowfullscreen></iframe>`;
},
},
];
return [
{
type: "lang",
regex: /{% youtube (.+?) %}/g,
replace: (_match: any, url: string) => {
const videoId = url.split("v=")[1] || url.split("/").pop();
return `<iframe width="560" height="315" src="https://www.youtube.com/embed/${videoId}" frameborder="0" allowfullscreen></iframe>`;
},
},
];
}

registerExtension("youtube", youtube());
Expand Down
Loading

0 comments on commit ffae045

Please sign in to comment.