Fix greentext processor

This commit is contained in:
silverpill 2022-05-16 01:23:07 +00:00
parent caf7b28b22
commit dee1cfa691
3 changed files with 16 additions and 4 deletions

View file

@ -231,6 +231,7 @@ import { useCurrentUser } from "@/store/user"
import { CRYPTOCURRENCIES } from "@/utils/cryptocurrencies"
import { getWallet } from "@/utils/ethereum"
import { formatDate } from "@/utils/format"
import { addGreentext } from "@/utils/greentext"
interface PaymentOption {
code: string;
@ -311,10 +312,7 @@ export default class PostComponent extends Vue {
}
get content(): string {
// Add greentext
const greentextRegexp = /(?<=^|>)(&gt;.+)(?=$|<)/gm
const content = this.post.content
.replaceAll(greentextRegexp, '<span class="greentext">$1</span>')
const content = addGreentext(this.post.content)
return content
}

4
src/utils/greentext.ts Normal file
View file

@ -0,0 +1,4 @@
export function addGreentext(text: string): string {
const greentextRegexp = /(?<=^|>)(&gt;[^<]+)(?=$|<)/gm
return text.replace(greentextRegexp, '<span class="greentext">$1</span>')
}

View file

@ -0,0 +1,10 @@
import { expect } from "chai"
import { addGreentext } from "@/utils/greentext"
describe("Greentext", () => {
it("Should add greentext class", () => {
const text = "test<br>&gt;greentext1<br>abc<br>&gt;greentext2<br>xyz"
const result = addGreentext(text)
expect(result).to.equal('test<br><span class="greentext">&gt;greentext1</span><br>abc<br><span class="greentext">&gt;greentext2</span><br>xyz')
})
})