-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 添加 NitWikit 插件转换器以增强 Markdown 处理能力
- Loading branch information
1 parent
56c9c20
commit 461d974
Showing
7 changed files
with
120 additions
and
58 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
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,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, | ||
}; | ||
} | ||
} |
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,4 @@ | ||
export * from "./transformer/assets"; | ||
export * from "./transformer/url"; | ||
export * from "./transformer/content"; | ||
export * from "./transformer/bilibili"; |
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,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 />`; | ||
} | ||
} | ||
} | ||
} |
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,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}" />`; | ||
}); | ||
} | ||
} | ||
} |
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,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 '(@theme\/[\s\S]+)';/g, "") | ||
.replace(/values={\[([\s\S]*)\]}/g, ""); | ||
} | ||
} | ||
} |
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,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, "/")); | ||
}); | ||
} | ||
} | ||
} |