-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
content-parser.ts
92 lines (87 loc) · 3.22 KB
/
content-parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Service, { service } from '@ember/service';
import MessagesService from './messages';
import { parseVideo } from './content-parser/video';
import { parseUrl } from './content-parser/url';
import { parseSimpleTags } from './content-parser/simple-tags';
import { parseImg } from './content-parser/img';
import { parseQuote } from './content-parser/quote';
import { parseList } from './content-parser/list';
import { parseTable } from './content-parser/table';
import { sanitize } from './content-parser/sanitize';
import { parseCode } from './content-parser/code';
import { parsePrivateMessageHtml } from './content-parser/private-message';
import SettingsService from './settings';
import { parsePrivilegedTags } from './content-parser/privileged-tags';
import { appConfig } from 'potber-client/config/app.config';
import { emojis } from 'potber-client/config/icons.config';
import { getEmojiFilename } from 'potber-client/utils/icons';
import { parseTex } from './content-parser/tex';
export default class ContentParserService extends Service {
@service declare messages: MessagesService;
@service declare settings: SettingsService;
/**
* Parses post content to HTML and returns the result.
* @param input The post content containing BBCode and other things.
* @param options.groupId (optional) The author's group id. Will be used to decide whether specific tags should be parsed at all (e.g. `[mod]`).
* Defaults to '3', the value for normal users.
* @returns The HTML output.
*/
parsePostContent(input: string, options?: { groupId?: string }) {
const { groupId } = { groupId: appConfig.standardUserGroupId, ...options };
let output = input;
output = sanitize(output);
output = parseCode(output);
output = parseSimpleTags(output);
output = parseTex(output);
output = parsePrivilegedTags(output, groupId);
output = parseImg(output);
output = parseVideo(output, window.location);
output = parseUrl(output, {
replaceForumUrls: this.settings.getSetting('replaceForumUrls'),
});
output = parseQuote(output, window.location);
output = parseList(output);
output = parseTable(output);
output = this.parseEmojis(output);
output = this.format(output);
return output;
}
/**
* Parses emojis.
* @param input The input string.
* @returns The output string.
*/
private parseEmojis(input: string) {
let output = input;
for (const emoji of emojis) {
output = output.replaceAll(
emoji.pattern,
`<img class="post-emoji" src="/images/post-emojis/${getEmojiFilename(
emoji.key,
)}" alt="${emoji.key}"/>`,
);
}
return output;
}
/**
* Formats the content and parses line breaks.
* @param input The input string.
* @returns The output string.
*/
format(input: string): string {
let output = input;
output = output.replaceAll(/\r\n/g, '<br/>');
output = output.replaceAll(/\n/g, '<br/>');
return output;
}
/**
* Parses the content of private messages.
* @param input The input string.
* @returns The output string.
*/
parsePrivateMessageContent(input: string): string {
let output = parsePrivateMessageHtml(input);
output = this.format(output);
return output;
}
}