Introduce processor
This commit is contained in:
parent
68ce6d5748
commit
90f8fe7e53
582 changed files with 246 additions and 188 deletions
14
src/server/api/common/text/elements/bold.ts
Normal file
14
src/server/api/common/text/elements/bold.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* Bold
|
||||
*/
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^\*\*(.+?)\*\*/);
|
||||
if (!match) return null;
|
||||
const bold = match[0];
|
||||
return {
|
||||
type: 'bold',
|
||||
content: bold,
|
||||
bold: bold.substr(2, bold.length - 4)
|
||||
};
|
||||
};
|
||||
17
src/server/api/common/text/elements/code.ts
Normal file
17
src/server/api/common/text/elements/code.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Code (block)
|
||||
*/
|
||||
|
||||
import genHtml from '../core/syntax-highlighter';
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^```([\s\S]+?)```/);
|
||||
if (!match) return null;
|
||||
const code = match[0];
|
||||
return {
|
||||
type: 'code',
|
||||
content: code,
|
||||
code: code.substr(3, code.length - 6).trim(),
|
||||
html: genHtml(code.substr(3, code.length - 6).trim())
|
||||
};
|
||||
};
|
||||
14
src/server/api/common/text/elements/emoji.ts
Normal file
14
src/server/api/common/text/elements/emoji.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* Emoji
|
||||
*/
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^:[a-zA-Z0-9+-_]+:/);
|
||||
if (!match) return null;
|
||||
const emoji = match[0];
|
||||
return {
|
||||
type: 'emoji',
|
||||
content: emoji,
|
||||
emoji: emoji.substr(1, emoji.length - 2)
|
||||
};
|
||||
};
|
||||
19
src/server/api/common/text/elements/hashtag.ts
Normal file
19
src/server/api/common/text/elements/hashtag.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Hashtag
|
||||
*/
|
||||
|
||||
module.exports = (text, i) => {
|
||||
if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null;
|
||||
const isHead = text[0] == '#';
|
||||
const hashtag = text.match(/^\s?#[^\s]+/)[0];
|
||||
const res: any[] = !isHead ? [{
|
||||
type: 'text',
|
||||
content: text[0]
|
||||
}] : [];
|
||||
res.push({
|
||||
type: 'hashtag',
|
||||
content: isHead ? hashtag : hashtag.substr(1),
|
||||
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
|
||||
});
|
||||
return res;
|
||||
};
|
||||
17
src/server/api/common/text/elements/inline-code.ts
Normal file
17
src/server/api/common/text/elements/inline-code.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Code (inline)
|
||||
*/
|
||||
|
||||
import genHtml from '../core/syntax-highlighter';
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^`(.+?)`/);
|
||||
if (!match) return null;
|
||||
const code = match[0];
|
||||
return {
|
||||
type: 'inline-code',
|
||||
content: code,
|
||||
code: code.substr(1, code.length - 2).trim(),
|
||||
html: genHtml(code.substr(1, code.length - 2).trim())
|
||||
};
|
||||
};
|
||||
19
src/server/api/common/text/elements/link.ts
Normal file
19
src/server/api/common/text/elements/link.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Link
|
||||
*/
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^\??\[([^\[\]]+?)\]\((https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+?)\)/);
|
||||
if (!match) return null;
|
||||
const silent = text[0] == '?';
|
||||
const link = match[0];
|
||||
const title = match[1];
|
||||
const url = match[2];
|
||||
return {
|
||||
type: 'link',
|
||||
content: link,
|
||||
title: title,
|
||||
url: url,
|
||||
silent: silent
|
||||
};
|
||||
};
|
||||
17
src/server/api/common/text/elements/mention.ts
Normal file
17
src/server/api/common/text/elements/mention.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Mention
|
||||
*/
|
||||
import parseAcct from '../../../../common/user/parse-acct';
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^(?:@[a-zA-Z0-9\-]+){1,2}/);
|
||||
if (!match) return null;
|
||||
const mention = match[0];
|
||||
const { username, host } = parseAcct(mention.substr(1));
|
||||
return {
|
||||
type: 'mention',
|
||||
content: mention,
|
||||
username,
|
||||
host
|
||||
};
|
||||
};
|
||||
14
src/server/api/common/text/elements/quote.ts
Normal file
14
src/server/api/common/text/elements/quote.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* Quoted text
|
||||
*/
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^"([\s\S]+?)\n"/);
|
||||
if (!match) return null;
|
||||
const quote = match[0];
|
||||
return {
|
||||
type: 'quote',
|
||||
content: quote,
|
||||
quote: quote.substr(1, quote.length - 2).trim(),
|
||||
};
|
||||
};
|
||||
14
src/server/api/common/text/elements/url.ts
Normal file
14
src/server/api/common/text/elements/url.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* URL
|
||||
*/
|
||||
|
||||
module.exports = text => {
|
||||
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/);
|
||||
if (!match) return null;
|
||||
const url = match[0];
|
||||
return {
|
||||
type: 'url',
|
||||
content: url,
|
||||
url: url
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue