summaryrefslogtreecommitdiff
path: root/src/server/api/common/text/elements
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/common/text/elements')
-rw-r--r--src/server/api/common/text/elements/bold.ts14
-rw-r--r--src/server/api/common/text/elements/code.ts17
-rw-r--r--src/server/api/common/text/elements/emoji.ts14
-rw-r--r--src/server/api/common/text/elements/hashtag.ts19
-rw-r--r--src/server/api/common/text/elements/inline-code.ts17
-rw-r--r--src/server/api/common/text/elements/link.ts19
-rw-r--r--src/server/api/common/text/elements/mention.ts17
-rw-r--r--src/server/api/common/text/elements/quote.ts14
-rw-r--r--src/server/api/common/text/elements/url.ts14
9 files changed, 145 insertions, 0 deletions
diff --git a/src/server/api/common/text/elements/bold.ts b/src/server/api/common/text/elements/bold.ts
new file mode 100644
index 0000000000..ce25764457
--- /dev/null
+++ b/src/server/api/common/text/elements/bold.ts
@@ -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)
+ };
+};
diff --git a/src/server/api/common/text/elements/code.ts b/src/server/api/common/text/elements/code.ts
new file mode 100644
index 0000000000..4821e95fe2
--- /dev/null
+++ b/src/server/api/common/text/elements/code.ts
@@ -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())
+ };
+};
diff --git a/src/server/api/common/text/elements/emoji.ts b/src/server/api/common/text/elements/emoji.ts
new file mode 100644
index 0000000000..e24231a223
--- /dev/null
+++ b/src/server/api/common/text/elements/emoji.ts
@@ -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)
+ };
+};
diff --git a/src/server/api/common/text/elements/hashtag.ts b/src/server/api/common/text/elements/hashtag.ts
new file mode 100644
index 0000000000..ee57b140b8
--- /dev/null
+++ b/src/server/api/common/text/elements/hashtag.ts
@@ -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;
+};
diff --git a/src/server/api/common/text/elements/inline-code.ts b/src/server/api/common/text/elements/inline-code.ts
new file mode 100644
index 0000000000..9f9ef51a2b
--- /dev/null
+++ b/src/server/api/common/text/elements/inline-code.ts
@@ -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())
+ };
+};
diff --git a/src/server/api/common/text/elements/link.ts b/src/server/api/common/text/elements/link.ts
new file mode 100644
index 0000000000..35563ddc3d
--- /dev/null
+++ b/src/server/api/common/text/elements/link.ts
@@ -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
+ };
+};
diff --git a/src/server/api/common/text/elements/mention.ts b/src/server/api/common/text/elements/mention.ts
new file mode 100644
index 0000000000..2025dfdaad
--- /dev/null
+++ b/src/server/api/common/text/elements/mention.ts
@@ -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
+ };
+};
diff --git a/src/server/api/common/text/elements/quote.ts b/src/server/api/common/text/elements/quote.ts
new file mode 100644
index 0000000000..cc8cfffdc4
--- /dev/null
+++ b/src/server/api/common/text/elements/quote.ts
@@ -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(),
+ };
+};
diff --git a/src/server/api/common/text/elements/url.ts b/src/server/api/common/text/elements/url.ts
new file mode 100644
index 0000000000..1003aff9c3
--- /dev/null
+++ b/src/server/api/common/text/elements/url.ts
@@ -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
+ };
+};