Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #230 #232

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/utils/bbcode/__test__/html.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { STICKER_DOMAIN_URL } from '../constants';
import { renderNodes, renderNode, render } from '../html';
import { renderNodes, renderNode, render, renderWithParser } from '../html';
import { Parser } from '../parser';
import type { VNode } from '../types';

describe('html render vnode', () => {
Expand Down Expand Up @@ -239,4 +240,27 @@ describe('html render bbcode string', () => {
'<div class="codeHighlight"><pre>ss[b]加粗\n换行了[/b](bgm38) [/fafa [code]</pre></div>',
);
});

test('render code by custom converter, nested', () => {
const input = '[b][url]http://qq.com[/url][/b]';
expect(
render(input, {
url: (node) => {
return '[url]convert map[/url]';
},
}),
).toBe('<strong>[url]convert map[/url]</strong>');
});
test('render code with Parser, nested', () => {
const input = '[b]加粗[size=16]16px[img]http://chii.in/img/ico/bgm88-31.gif[/img][/size][/b]';
expect(
renderWithParser(new Parser(input, [], ['img']), {
url: (node) => {
return '[url]convert map[/url]';
},
}),
).toBe(
'<strong>加粗<span style="font-size:16px;line-height:16px">16px[img]http://chii.in/img/ico/bgm88-31.gif[/img]</span></strong>',
);
});
});
36 changes: 36 additions & 0 deletions packages/utils/bbcode/__test__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ describe('bbcode parser', () => {
];
expect(getNodes(input)).toEqual(expect.arrayContaining(tests));
});
test('ignore img bbcode', () => {
const input = `存放于其他网络服务器的图片:
[img]http://chii.in/img/ico/bgm88-31.gif[/img]`;
const tests: CodeNodeTypes[] = [
'存放于其他网络服务器的图片:\n',
'[img]http://chii.in/img/ico/bgm88-31.gif[/img]',
];
expect(new Parser(input, [], ['img']).parse()).toEqual(expect.arrayContaining(tests));
});
test('simple bbcode', () => {
const input = `我是[mask]马赛克文字[/mask]
[s]删除线文字[/s]
Expand Down Expand Up @@ -320,6 +329,33 @@ describe('bbcode parser', () => {
]),
);
});
test('merge tags with ignore case', () => {
const fn = (): boolean => true;
const tags = mergeTags(
[
'i',
'b',
{
name: 's',
schema: {
s: fn,
},
},
],
[
{
name: 'i',
schema: {
i: fn,
},
},
's',
'mybbcode',
],
['i', 's'],
);
expect(tags).toEqual(expect.arrayContaining(['b', 'mybbcode']));
});
test('subject bbcode', () => {
const tests: Array<[string, CodeNodeTypes[]]> = [
[
Expand Down
13 changes: 8 additions & 5 deletions packages/utils/bbcode/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ function toVNode(
return vnode;
}

let UserConverterFnMap: Record<string, ConverterFn> = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好不要这么写吧,跟现在一样是纯函数比较好...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成类了


export function setUserConverter(map: Record<string, ConverterFn>): void {
UserConverterFnMap = map;
}

const CONVERTER_FN_MAP: Record<string, ConverterFn> = {
b: (node) => toVNode(node, 'strong'),
i: (node) => toVNode(node, 'em'),
Expand Down Expand Up @@ -233,14 +239,11 @@ const CONVERTER_FN_MAP: Record<string, ConverterFn> = {
user: convertUser,
};

export function convert(
node: CodeNodeTypes,
converterMap: Record<string, ConverterFn> = {},
): NodeTypes {
export function convert(node: CodeNodeTypes): NodeTypes {
if (typeof node === 'string') {
return node;
}
let converterFn = converterMap[node.type];
let converterFn = UserConverterFnMap[node.type];
if (!converterFn) {
converterFn = CONVERTER_FN_MAP[node.type];
}
Expand Down
16 changes: 13 additions & 3 deletions packages/utils/bbcode/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UnreadableCodeError } from '../index';
import { convert } from './convert';
import { convert, setUserConverter } from './convert';
import { Parser } from './parser';
import type { CodeNodeTypes, ConverterFn, NodeTypes, VNode } from './types';

Expand Down Expand Up @@ -84,10 +84,20 @@ export function renderNodes(nodes: NodeTypes[], parentNode?: VNode): string {
}

export function render(rawStr: string, converterMap: Record<string, ConverterFn> = {}): string {
return renderWithParser(new Parser(rawStr), converterMap);
}

export function renderWithParser(
parser: Parser,
converterMap: Record<string, ConverterFn> = {},
): string {
let result = '';
const nodes: CodeNodeTypes[] = new Parser(rawStr).parse();
const nodes: CodeNodeTypes[] = parser.parse();
setUserConverter(converterMap);
nodes.forEach((node) => {
result += renderNode(convert(node, converterMap));
result += renderNode(convert(node));
});
// 重置为默认值 @TODO 改成类避免使用全局变量
setUserConverter({});
return result;
}
2 changes: 1 addition & 1 deletion packages/utils/bbcode/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { render } from './html';
export { render, renderWithParser } from './html';
export * from './parser';
export { convert } from './convert';
export * from './types';
21 changes: 16 additions & 5 deletions packages/utils/bbcode/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,23 @@ const DEFAULT_TAGS: ITag[] = [
];

// 合并 tag 配置。新的覆盖旧的
export function mergeTags(tagList: ITag[], toMergeTags: ITag[]): ITag[] {
const results: ITag[] = [...toMergeTags];
tagList.forEach((tag) => {
export function mergeTags(
tagList: ITag[],
toMergeTags: ITag[],
ignoreTagNames: string[] = [],
): ITag[] {
let results: ITag[] = [...toMergeTags];
const getTagName = (tag: ITag) => {
let name = '';
if (typeof tag === 'string') {
name = tag;
} else {
name = tag.name;
}
return name;
};
tagList.forEach((tag) => {
const name = getTagName(tag);
const idx = results.findIndex((t) => {
if (typeof t === 'string') {
return t === name;
Expand All @@ -142,6 +150,9 @@ export function mergeTags(tagList: ITag[], toMergeTags: ITag[]): ITag[] {
results.push(tag);
}
});
results = results.filter((tag) => {
return !ignoreTagNames.includes(getTagName(tag));
});
return results;
}

Expand All @@ -153,13 +164,13 @@ export class Parser {
private readonly tagStack: string[];
private readonly validTags: ITag[];

constructor(input: string, tags: ITag[] = []) {
constructor(input: string, tags: ITag[] = [], ignoreTagNames: string[] = []) {
this.input = input;
this.pos = 0;
this.ctxStack = [];
this.tagStack = [];
// 解析器支持的 tag; sticker 用来表示 Bangumi 的表情,不是 bbcode
this.validTags = mergeTags(DEFAULT_TAGS, tags);
this.validTags = mergeTags(DEFAULT_TAGS, tags, ignoreTagNames);
}

parse(): CodeNodeTypes[] {
Expand Down