Skip to content

Commit

Permalink
feat: 添加 NitWikit 插件转换器以增强 Markdown 处理能力
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanzisama committed Dec 25, 2024
1 parent 56c9c20 commit 461d974
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 58 deletions.
63 changes: 5 additions & 58 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getSidebar } from "./utils/vitepress";
import themeConfig from "./theme.config";
import { resolve } from "path";
import { withMermaid } from "vitepress-plugin-mermaid";
import { NitWikitAssertsTransformer, NitWikitBilibiliTransformer, NitWikitContentTransformer, NitWikitUrlTransformer } from "../build/plugins";

const markdownRegExp = {
image: /!\[(?<name>.*)\]\((?<url>(.*))\)/g,
Expand All @@ -25,64 +26,10 @@ export default withMermaid({
publicDir: "../public",
assetsInclude: ["**/*.JPG", "**/*.PNG"],
plugins: [
{
name: "nitwikit-transformer",
enforce: "pre",
transform(code, id) {
if (id.endsWith(".md")) {
return code
.replace(/import ([\s\S]+) from ['"](@theme\/[\s\S]+)['"];/g, "")
.replace(/import ([\s\S]+) from &#39;(@theme\/[\s\S]+)&#39;;/g, "")
.replace(/values={\[([\s\S]*)\]}/g, "");
}
},
},
{
name: "nitwikit-content-transformer",
enforce: "pre",
transform(code, id) {
if (id.endsWith(".md")) {
const result = code.match(markdownRegExp.title);
if (result !== null) {
console.info(id, result.groups?.title);
return code.replace(`${result.groups?.heading}${result.groups?.title}`, `# ${result.groups?.title}`);
}
}
},
},
{
name: "nitwikit-url-transformer",
enforce: "pre",
transform(code, id) {
if (id.endsWith(".md")) {
return code.replace(markdownRegExp.nitwikitUrl, (match, _name, url) => {
return match.replace(url, url.replace("https://nitwikit.yizhan.wiki/", "/"));
});
}
},
},
{
name: "nitwikit-assets-transformer",
enforce: "pre",
transform(code, id) {
if (id.endsWith(".md")) {
if (code.match(markdownRegExp.image)) {
return code + `\n<nw-image-viewer />`;
}
}
},
},
{
name: "nitwikit-bilitv-transformer",
enforce: "pre",
transform(code, id) {
if (id.endsWith(".md")) {
return code.replace(markdownRegExp.bilitv, (_match, name, url, bvId) => {
return `<bili-player title="${name}" src="${url}" bv-id="${bvId}" />`;
});
}
},
},
new NitWikitContentTransformer().build(),
new NitWikitAssertsTransformer().build(),
new NitWikitUrlTransformer().build(),
new NitWikitBilibiliTransformer().build(),
],
css: {
preprocessorOptions: {
Expand Down
23 changes: 23 additions & 0 deletions build/plugins/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Plugin } from "vite";

export abstract class BasePlugin {
#name: Plugin["name"];
#enforce: Plugin["enforce"];

constructor(options: { name: string; enforce: "pre" | "post" }) {
this.#name = options.name;
this.#enforce = options.enforce;
}

transform(code: string, id: string): string | Promise<string> | void {
return code;
}

build(): Plugin {
return {
name: this.#name,
enforce: this.#enforce,
transform: this.transform,
};
}
}
4 changes: 4 additions & 0 deletions build/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./transformer/assets";
export * from "./transformer/url";
export * from "./transformer/content";
export * from "./transformer/bilibili";
20 changes: 20 additions & 0 deletions build/plugins/transformer/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BasePlugin } from "../base";

export class NitWikitAssertsTransformer extends BasePlugin {
static #regExp: RegExp = /!\[([^\]]*)\]\(([^)]*)\)/g;

constructor() {
super({
name: "nitwikit-assets-transformer",
enforce: "pre",
});
}

override transform(code: string, id: string): string | Promise<string> | void {
if (id.endsWith(".md")) {
if (code.match(NitWikitAssertsTransformer.#regExp)) {
return code + `\n<nw-image-viewer />`;
}
}
}
}
20 changes: 20 additions & 0 deletions build/plugins/transformer/bilibili.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BasePlugin } from "../base";

export class NitWikitBilibiliTransformer extends BasePlugin {
static #regExp: RegExp = /\[(?<name>.*)\]\((?<url>https:\/\/www.bilibili.com\/video\/(?<id>(.*))\/(.*))\)/g;

constructor() {
super({
name: "nitwikit-bilibili-transformer",
enforce: "pre",
});
}

override transform(code: string, id: string): string | Promise<string> | void {
if (id.endsWith(".md")) {
return code.replace(NitWikitBilibiliTransformer.#regExp, (_match, name, url, bvId) => {
return `<bili-player title="${name}" src="${url}" bv-id="${bvId}" />`;
});
}
}
}
27 changes: 27 additions & 0 deletions build/plugins/transformer/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BasePlugin } from "../base";

export class NitWikitContentTransformer extends BasePlugin {
static #regExp: RegExp = /^(---([\s\S]*)---)?([\r|\n]*)(?<heading>[#]{2,6} )(?<title>[\s\S].*)/;

constructor() {
super({
name: "nitwikit-content-transformer",
enforce: "pre",
});
}

override transform(code: string, id: string): string | Promise<string> | void {
if (id.endsWith(".md")) {
const result = code.match(NitWikitContentTransformer.#regExp);
let newCode = code;
if (result !== null) {
newCode = code.replace(`${result.groups?.heading}${result.groups?.title}`, `# ${result.groups?.title}`);
}

return newCode
.replace(/import ([\s\S]+) from ['"](@theme\/[\s\S]+)['"];/g, "")
.replace(/import ([\s\S]+) from &#39;(@theme\/[\s\S]+)&#39;;/g, "")
.replace(/values={\[([\s\S]*)\]}/g, "");
}
}
}
21 changes: 21 additions & 0 deletions build/plugins/transformer/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BasePlugin } from "../base";

export class NitWikitUrlTransformer extends BasePlugin {
static #regExp: RegExp = /\[(?<name>.*)\]\((?<url>https:\/\/nitwikit\.yizhan\.wiki\/(.*))\)/g;
static #url: string = "https://nitwikit.yizhan.wiki/";

constructor() {
super({
name: "nitwikit-url-transformer",
enforce: "pre",
});
}

override transform(code: string, id: string): string | Promise<string> | void {
if (id.endsWith(".md")) {
return code.replace(NitWikitUrlTransformer.#regExp, (match, _name, url) => {
return match.replace(url, url.replace(NitWikitUrlTransformer.#url, "/"));
});
}
}
}

0 comments on commit 461d974

Please sign in to comment.