mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
669 B
TypeScript
31 lines
669 B
TypeScript
import bbcodeParser from './parser';
|
|
import type { AstNode } from './type';
|
|
import _isNil from 'lodash/isNil';
|
|
|
|
function bbcodeNodeToPlainText(node: AstNode): string {
|
|
if (_isNil(node)) {
|
|
return '';
|
|
}
|
|
|
|
if (typeof node === 'string') {
|
|
return String(node);
|
|
} else {
|
|
if (node.tag === 'img') {
|
|
return '[图片]';
|
|
} else {
|
|
return (node.content ?? [])
|
|
.map((sub) => bbcodeNodeToPlainText(sub))
|
|
.join('');
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将 BBCode 转化为普通的字符串
|
|
*/
|
|
export function bbcodeToPlainText(bbcode: string): string {
|
|
const ast = bbcodeParser.parse(bbcode);
|
|
|
|
return ast.map(bbcodeNodeToPlainText).join('');
|
|
}
|