Use provider to process content

This commit is contained in:
zhengyi 2023-12-18 18:31:24 +08:00
parent 8dbb28ad88
commit 849f012b84
5 changed files with 75 additions and 14 deletions

View File

@ -1,20 +1,12 @@
import "./less/tips.less"
import "./less/render.less"
import {RENDER_CLASS} from "./constant";
import {ProviderFactory} from "./provider/provider";
export const haloRender = (src: string): string => {
src = src.trim()
const lines = src.split("\n");
let html: string = ""
const type = lines[0]
if (type.startsWith("tips")) {
html = `<div class="${RENDER_CLASS} tips ${type.replace(":", "-")}">`
lines.forEach((line: string, index: number) => {
if (index === 0) return
if (line) html += `<div>${line}</div>`
})
html += "</div>"
return html
}
return "<p>Working</p>"
const line = src.split("\n");
const type = line[0]
line.splice(0, 1)
const content = line.join("\n")
return ProviderFactory.getFactory().process(type, content)
}

View File

@ -1,3 +1,11 @@
.halo-render {
margin: 5px;
}
.invalid-type {
color: red;
margin: 5px;
text-align: center;
padding: 10px;
background-color: lightpink;
}

View File

@ -0,0 +1,7 @@
import {TipsProvider} from "./tips-provider";
import {Provider} from "./provider";
import {GitProvider} from "./git-provider";
export const ProviderList:Array<Provider> = [
new TipsProvider()
]

34
src/provider/provider.ts Normal file
View File

@ -0,0 +1,34 @@
import {TipsProvider} from "./tips-provider";
import {ProviderList} from "./provider-list";
export declare interface Provider {
check: (type: string) => boolean
process: (type:string,content: string) => string
}
export class ProviderFactory {
static manager: ProviderFactory | null = null
private providers: Array<Provider>
constructor(providers: Array<Provider>) {
this.providers = providers;
}
process(type: string, content: string) {
let html = "<h2 class='invalid-type'>Invalid Type!</h2>"
this.providers.forEach(provider => {
if (provider.check(type)) {
html = provider.process(type, content)
return
}
})
return html
}
static getFactory() {
if (!this.manager) {
this.manager = new ProviderFactory(ProviderList)
}
return this.manager
}
}

View File

@ -0,0 +1,20 @@
import {Provider} from "./provider";
import "../less/tips.less"
import {RENDER_CLASS} from "../constant";
export class TipsProvider implements Provider {
check(type: string): boolean {
return type.startsWith("tips")
}
process(type: string, content: string): string {
const lines = content.split("\n")
let html = ""
html = `<div class="${RENDER_CLASS} tips ${type.replace(":", "-")}">`
lines.forEach((line: string) => {
if (line) html += `<div>${line}</div>`
})
html += "</div>"
return html
}
}