summaryrefslogtreecommitdiff
path: root/src/mfm/parse/elements
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-06-21 01:21:57 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-06-21 01:21:57 +0900
commit79d1bf30a49e1fd1ef1f8b743a9aff84d104fb89 (patch)
treeed8f50f01cc50cf851dc0d0f399ed7e0f514f6c6 /src/mfm/parse/elements
parentDisable transitions to avoid memory leak (diff)
downloadsharkey-79d1bf30a49e1fd1ef1f8b743a9aff84d104fb89.tar.gz
sharkey-79d1bf30a49e1fd1ef1f8b743a9aff84d104fb89.tar.bz2
sharkey-79d1bf30a49e1fd1ef1f8b743a9aff84d104fb89.zip
リモートユーザーのHTMLで表現されたプロフィールをMFMに変換するように
Diffstat (limited to 'src/mfm/parse/elements')
-rw-r--r--src/mfm/parse/elements/bold.ts20
-rw-r--r--src/mfm/parse/elements/code.ts24
-rw-r--r--src/mfm/parse/elements/emoji.ts20
-rw-r--r--src/mfm/parse/elements/hashtag.ts25
-rw-r--r--src/mfm/parse/elements/inline-code.ts24
-rw-r--r--src/mfm/parse/elements/link.ts27
-rw-r--r--src/mfm/parse/elements/mention.ts24
-rw-r--r--src/mfm/parse/elements/quote.ts20
-rw-r--r--src/mfm/parse/elements/search.ts19
-rw-r--r--src/mfm/parse/elements/title.ts20
-rw-r--r--src/mfm/parse/elements/url.ts20
11 files changed, 243 insertions, 0 deletions
diff --git a/src/mfm/parse/elements/bold.ts b/src/mfm/parse/elements/bold.ts
new file mode 100644
index 0000000000..cf615cd3cc
--- /dev/null
+++ b/src/mfm/parse/elements/bold.ts
@@ -0,0 +1,20 @@
+/**
+ * Bold
+ */
+
+export type TextElementBold = {
+ type: 'bold'
+ content: string
+ bold: string
+};
+
+export default function(text: string) {
+ const match = text.match(/^\*\*(.+?)\*\*/);
+ if (!match) return null;
+ const bold = match[0];
+ return {
+ type: 'bold',
+ content: bold,
+ bold: bold.substr(2, bold.length - 4)
+ } as TextElementBold;
+}
diff --git a/src/mfm/parse/elements/code.ts b/src/mfm/parse/elements/code.ts
new file mode 100644
index 0000000000..f48e945048
--- /dev/null
+++ b/src/mfm/parse/elements/code.ts
@@ -0,0 +1,24 @@
+/**
+ * Code (block)
+ */
+
+import genHtml from '../core/syntax-highlighter';
+
+export type TextElementCode = {
+ type: 'code'
+ content: string
+ code: string
+ html: string
+};
+
+export default function(text: string) {
+ 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())
+ } as TextElementCode;
+}
diff --git a/src/mfm/parse/elements/emoji.ts b/src/mfm/parse/elements/emoji.ts
new file mode 100644
index 0000000000..83d3effef5
--- /dev/null
+++ b/src/mfm/parse/elements/emoji.ts
@@ -0,0 +1,20 @@
+/**
+ * Emoji
+ */
+
+export type TextElementEmoji = {
+ type: 'emoji'
+ content: string
+ emoji: string
+};
+
+export default function(text: string) {
+ 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)
+ } as TextElementEmoji;
+}
diff --git a/src/mfm/parse/elements/hashtag.ts b/src/mfm/parse/elements/hashtag.ts
new file mode 100644
index 0000000000..129041774f
--- /dev/null
+++ b/src/mfm/parse/elements/hashtag.ts
@@ -0,0 +1,25 @@
+/**
+ * Hashtag
+ */
+
+export type TextElementHashtag = {
+ type: 'hashtag'
+ content: string
+ hashtag: string
+};
+
+export default function(text: string, i: number) {
+ 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 as TextElementHashtag[];
+}
diff --git a/src/mfm/parse/elements/inline-code.ts b/src/mfm/parse/elements/inline-code.ts
new file mode 100644
index 0000000000..1dd5affa51
--- /dev/null
+++ b/src/mfm/parse/elements/inline-code.ts
@@ -0,0 +1,24 @@
+/**
+ * Code (inline)
+ */
+
+import genHtml from '../core/syntax-highlighter';
+
+export type TextElementInlineCode = {
+ type: 'inline-code'
+ content: string
+ code: string
+ html: string
+};
+
+export default function(text: string) {
+ 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())
+ } as TextElementInlineCode;
+}
diff --git a/src/mfm/parse/elements/link.ts b/src/mfm/parse/elements/link.ts
new file mode 100644
index 0000000000..b353aebc5c
--- /dev/null
+++ b/src/mfm/parse/elements/link.ts
@@ -0,0 +1,27 @@
+/**
+ * Link
+ */
+
+export type TextElementLink = {
+ type: 'link'
+ content: string
+ title: string
+ url: string
+ silent: boolean
+};
+
+export default function(text: string) {
+ 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
+ } as TextElementLink;
+}
diff --git a/src/mfm/parse/elements/mention.ts b/src/mfm/parse/elements/mention.ts
new file mode 100644
index 0000000000..eda60b530a
--- /dev/null
+++ b/src/mfm/parse/elements/mention.ts
@@ -0,0 +1,24 @@
+/**
+ * Mention
+ */
+import parseAcct from '../../../acct/parse';
+
+export type TextElementMention = {
+ type: 'mention'
+ content: string
+ username: string
+ host: string
+};
+
+export default function(text: string) {
+ const match = text.match(/^@[a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9])?/i);
+ if (!match) return null;
+ const mention = match[0];
+ const { username, host } = parseAcct(mention.substr(1));
+ return {
+ type: 'mention',
+ content: mention,
+ username,
+ host
+ } as TextElementMention;
+}
diff --git a/src/mfm/parse/elements/quote.ts b/src/mfm/parse/elements/quote.ts
new file mode 100644
index 0000000000..bef9ad4988
--- /dev/null
+++ b/src/mfm/parse/elements/quote.ts
@@ -0,0 +1,20 @@
+/**
+ * Quoted text
+ */
+
+export type TextElementQuote = {
+ type: 'quote'
+ content: string
+ quote: string
+};
+
+export default function(text: string) {
+ 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(),
+ } as TextElementQuote;
+}
diff --git a/src/mfm/parse/elements/search.ts b/src/mfm/parse/elements/search.ts
new file mode 100644
index 0000000000..e5d9b9f0c2
--- /dev/null
+++ b/src/mfm/parse/elements/search.ts
@@ -0,0 +1,19 @@
+/**
+ * Search
+ */
+
+export type TextElementSearch = {
+ type: 'search'
+ content: string
+ query: string
+};
+
+export default function(text: string) {
+ const match = text.match(/^(.+?) 検索(\n|$)/);
+ if (!match) return null;
+ return {
+ type: 'search',
+ content: match[0],
+ query: match[1]
+ };
+}
diff --git a/src/mfm/parse/elements/title.ts b/src/mfm/parse/elements/title.ts
new file mode 100644
index 0000000000..b89739a7c5
--- /dev/null
+++ b/src/mfm/parse/elements/title.ts
@@ -0,0 +1,20 @@
+/**
+ * Title
+ */
+
+export type TextElementTitle = {
+ type: 'title'
+ content: string
+ title: string
+};
+
+export default function(text: string) {
+ const match = text.match(/^【(.+?)】\n/);
+ if (!match) return null;
+ const title = match[0];
+ return {
+ type: 'title',
+ content: title,
+ title: title.substr(1, title.length - 3)
+ } as TextElementTitle;
+}
diff --git a/src/mfm/parse/elements/url.ts b/src/mfm/parse/elements/url.ts
new file mode 100644
index 0000000000..78b9b1f2f1
--- /dev/null
+++ b/src/mfm/parse/elements/url.ts
@@ -0,0 +1,20 @@
+/**
+ * URL
+ */
+
+export type TextElementUrl = {
+ type: 'url'
+ content: string
+ url: string
+};
+
+export default function(text: string) {
+ const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/);
+ if (!match) return null;
+ const url = match[0];
+ return {
+ type: 'url',
+ content: url,
+ url: url
+ } as TextElementUrl;
+}