summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-06-18 08:42:17 +0900
committerGitHub <noreply@github.com>2018-06-18 08:42:17 +0900
commita766faeae9f5d5ea9be6f758ec446dbcd240a86e (patch)
treee2ed5f30e2776259ef961c9f9a82412e7f9f66cd /src
parentMerge pull request #1735 from rinsuki/fix/minor-fix-201806171721 (diff)
parent[noImplicitAny: true] src/services/drive (diff)
downloadsharkey-a766faeae9f5d5ea9be6f758ec446dbcd240a86e.tar.gz
sharkey-a766faeae9f5d5ea9be6f758ec446dbcd240a86e.tar.bz2
sharkey-a766faeae9f5d5ea9be6f758ec446dbcd240a86e.zip
Merge pull request #1738 from rinsuki/features/ts-noimplicitany-true
[WIP] noImplicitAny: true
Diffstat (limited to 'src')
-rw-r--r--src/build/fa.ts16
-rw-r--r--src/build/i18n.ts21
-rw-r--r--src/client/docs/api/gulpfile.ts19
-rw-r--r--src/client/docs/gulpfile.ts4
-rw-r--r--src/client/docs/vars.ts2
-rw-r--r--src/services/drive/add-file.ts8
-rw-r--r--src/services/drive/upload-from-url.ts4
-rw-r--r--src/services/note/create.ts4
-rw-r--r--src/services/note/reaction/create.ts2
-rw-r--r--src/text/html.ts5
-rw-r--r--src/text/parse/core/syntax-highlighter.ts13
-rw-r--r--src/text/parse/elements/bold.ts12
-rw-r--r--src/text/parse/elements/code.ts13
-rw-r--r--src/text/parse/elements/emoji.ts12
-rw-r--r--src/text/parse/elements/hashtag.ts12
-rw-r--r--src/text/parse/elements/inline-code.ts13
-rw-r--r--src/text/parse/elements/link.ts14
-rw-r--r--src/text/parse/elements/mention.ts13
-rw-r--r--src/text/parse/elements/quote.ts12
-rw-r--r--src/text/parse/elements/search.ts10
-rw-r--r--src/text/parse/elements/title.ts12
-rw-r--r--src/text/parse/elements/url.ts12
-rw-r--r--src/text/parse/index.ts39
23 files changed, 193 insertions, 79 deletions
diff --git a/src/build/fa.ts b/src/build/fa.ts
index 111c19ae66..077bb51e6d 100644
--- a/src/build/fa.ts
+++ b/src/build/fa.ts
@@ -3,18 +3,18 @@
*/
import * as fontawesome from '@fortawesome/fontawesome';
-import * as regular from '@fortawesome/fontawesome-free-regular';
-import * as solid from '@fortawesome/fontawesome-free-solid';
-import * as brands from '@fortawesome/fontawesome-free-brands';
+import regular from '@fortawesome/fontawesome-free-regular';
+import solid from '@fortawesome/fontawesome-free-solid';
+import brands from '@fortawesome/fontawesome-free-brands';
fontawesome.library.add(regular, solid, brands);
export const pattern = /%fa:(.+?)%/g;
-export const replacement = (match, key) => {
+export const replacement = (match: string, key: string) => {
const args = key.split(' ');
let prefix = 'fas';
- const classes = [];
+ const classes: string[] = [];
let transform = '';
let name;
@@ -34,12 +34,12 @@ export const replacement = (match, key) => {
}
});
- const icon = fontawesome.icon({ prefix, iconName: name }, {
- classes: classes
+ const icon = fontawesome.icon({ prefix, iconName: name } as fontawesome.IconLookup, {
+ classes: classes,
+ transform: fontawesome.parse.transform(transform)
});
if (icon) {
- icon.transform = fontawesome.parse.transform(transform);
return `<i data-fa class="${name}">${icon.html[0]}</i>`;
} else {
console.warn(`'${name}' not found in fa`);
diff --git a/src/build/i18n.ts b/src/build/i18n.ts
index 35854055d0..308ff0da00 100644
--- a/src/build/i18n.ts
+++ b/src/build/i18n.ts
@@ -2,7 +2,7 @@
* Replace i18n texts
*/
-import locale from '../../locales';
+import locale, { isAvailableLanguage, LocaleObject, LocaleObjectChildren } from '../../locales';
export default class Replacer {
private lang: string;
@@ -16,19 +16,19 @@ export default class Replacer {
this.replacement = this.replacement.bind(this);
}
- private get(path: string, key: string) {
- const texts = locale[this.lang];
-
- if (texts == null) {
+ private get(path: string, key: string): string {
+ if (!isAvailableLanguage(this.lang)) {
console.warn(`lang '${this.lang}' is not supported`);
return key; // Fallback
}
- let text = texts;
+ const texts = locale[this.lang];
+
+ let text: LocaleObjectChildren = texts;
if (path) {
if (text.hasOwnProperty(path)) {
- text = text[path];
+ text = text[path] as LocaleObject;
} else {
console.warn(`path '${path}' not found in '${this.lang}'`);
return key; // Fallback
@@ -38,7 +38,7 @@ export default class Replacer {
// Check the key existance
const error = key.split('.').some(k => {
if (text.hasOwnProperty(k)) {
- text = text[k];
+ text = (text as LocaleObject)[k];
return false;
} else {
return true;
@@ -48,12 +48,15 @@ export default class Replacer {
if (error) {
console.warn(`key '${key}' not found in '${path}' of '${this.lang}'`);
return key; // Fallback
+ } else if (typeof text !== "string") {
+ console.warn(`key '${key}' is not string in '${path}' of '${this.lang}'`);
+ return key; // Fallback
} else {
return text;
}
}
- public replacement(match, key) {
+ public replacement(match: string, key: string) {
let path = null;
if (key.indexOf('|') != -1) {
diff --git a/src/client/docs/api/gulpfile.ts b/src/client/docs/api/gulpfile.ts
index 9980ede231..0eb8b88287 100644
--- a/src/client/docs/api/gulpfile.ts
+++ b/src/client/docs/api/gulpfile.ts
@@ -19,9 +19,10 @@ import generateVars from '../vars';
const langs = Object.keys(locales);
-const kebab = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
+const kebab = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
-const parseParam = param => {
+// WIP type
+const parseParam = (param: any) => {
const id = param.type.match(/^id\((.+?)\)|^id/);
const entity = param.type.match(/^entity\((.+?)\)/);
const isObject = /^object/.test(param.type);
@@ -57,7 +58,7 @@ const parseParam = param => {
return param;
};
-const sortParams = params => {
+const sortParams = (params: Array<{name: string}>) => {
params.sort((a, b) => {
if (a.name < b.name)
return -1;
@@ -68,14 +69,15 @@ const sortParams = params => {
return params;
};
-const extractDefs = params => {
- let defs = [];
+// WIP type
+const extractDefs = (params: any[]) => {
+ let defs: any[] = [];
params.forEach(param => {
if (param.def) {
defs.push({
name: param.defName,
- params: sortParams(param.def.map(p => parseParam(p)))
+ params: sortParams(param.def.map((p: any) => parseParam(p)))
});
const childDefs = extractDefs(param.def);
@@ -109,8 +111,10 @@ gulp.task('doc:api:endpoints', async () => {
path: ep.endpoint
},
desc: ep.desc,
+ // @ts-ignore
params: sortParams(ep.params.map(p => parseParam(p))),
paramDefs: extractDefs(ep.params),
+ // @ts-ignore
res: ep.res ? sortParams(ep.res.map(p => parseParam(p))) : null,
resDefs: ep.res ? extractDefs(ep.res) : null,
};
@@ -155,7 +159,8 @@ gulp.task('doc:api:entities', async () => {
const vars = {
name: entity.name,
desc: entity.desc,
- props: sortParams(entity.props.map(p => parseParam(p))),
+ // WIP type
+ props: sortParams(entity.props.map((p: any) => parseParam(p))),
propDefs: extractDefs(entity.props),
};
langs.forEach(lang => {
diff --git a/src/client/docs/gulpfile.ts b/src/client/docs/gulpfile.ts
index 56bf6188c8..4683a04659 100644
--- a/src/client/docs/gulpfile.ts
+++ b/src/client/docs/gulpfile.ts
@@ -8,8 +8,8 @@ import * as glob from 'glob';
import * as gulp from 'gulp';
import * as pug from 'pug';
import * as mkdirp from 'mkdirp';
-import stylus = require('gulp-stylus');
-import cssnano = require('gulp-cssnano');
+const stylus = require('gulp-stylus');
+const cssnano = require('gulp-cssnano');
import I18nReplacer from '../../build/i18n';
import fa from '../../build/fa';
diff --git a/src/client/docs/vars.ts b/src/client/docs/vars.ts
index 32b961aaa9..93082767e3 100644
--- a/src/client/docs/vars.ts
+++ b/src/client/docs/vars.ts
@@ -38,7 +38,7 @@ export default async function(): Promise<{ [key: string]: any }> {
vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^h1 (.+?)\r?\n/)[1];
});
- vars['kebab'] = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
+ vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
vars['config'] = config;
diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts
index d7f71e8665..4167df0662 100644
--- a/src/services/drive/add-file.ts
+++ b/src/services/drive/add-file.ts
@@ -7,7 +7,7 @@ import * as crypto from 'crypto';
import * as _gm from 'gm';
import * as debug from 'debug';
import fileType = require('file-type');
-import prominence = require('prominence');
+const prominence = require('prominence');
import DriveFile, { IMetadata, getDriveFileBucket, IDriveFile } from '../../models/drive-file';
import DriveFolder from '../../models/drive-folder';
@@ -33,7 +33,7 @@ const writeChunks = (name: string, readable: stream.Readable, type: string, meta
readable.pipe(writeStream);
}));
-const writeThumbnailChunks = (name: string, readable: stream.Readable, originalId) =>
+const writeThumbnailChunks = (name: string, readable: stream.Readable, originalId: mongodb.ObjectID) =>
getDriveFileThumbnailBucket()
.then(bucket => new Promise((resolve, reject) => {
const writeStream = bucket.openUploadStream(name, {
@@ -89,7 +89,7 @@ export default async function(
const calcHash = new Promise<string>((res, rej) => {
const readable = fs.createReadStream(path);
const hash = crypto.createHash('md5');
- const chunks = [];
+ const chunks: Buffer[] = [];
readable
.on('error', rej)
.pipe(hash)
@@ -201,7 +201,7 @@ export default async function(
return driveFolder;
};
- const properties = {};
+ const properties: {[key: string]: any} = {};
let propPromises: Array<Promise<void>> = [];
diff --git a/src/services/drive/upload-from-url.ts b/src/services/drive/upload-from-url.ts
index e216ca603d..f83d57d415 100644
--- a/src/services/drive/upload-from-url.ts
+++ b/src/services/drive/upload-from-url.ts
@@ -8,10 +8,12 @@ import * as request from 'request';
import { IDriveFile, validateFileName } from '../../models/drive-file';
import create from './add-file';
import config from '../../config';
+import { IUser } from '../../models/user';
+import * as mongodb from "mongodb";
const log = debug('misskey:drive:upload-from-url');
-export default async (url: string, user, folderId = null, uri: string = null): Promise<IDriveFile> => {
+export default async (url: string, user: IUser, folderId: mongodb.ObjectID = null, uri: string = null): Promise<IDriveFile> => {
log(`REQUESTED: ${url}`);
let name = URL.parse(url).pathname.split('/').pop();
diff --git a/src/services/note/create.ts b/src/services/note/create.ts
index 98415b8971..ef03c4e044 100644
--- a/src/services/note/create.ts
+++ b/src/services/note/create.ts
@@ -33,7 +33,7 @@ class NotificationManager {
reason: Reason;
}> = [];
- constructor(user, note) {
+ constructor(user: IUser, note: any) {
this.user = user;
this.note = note;
}
@@ -451,7 +451,7 @@ export default async (user: IUser, data: {
// $ne: note._id
// }
//});
- const existRenote = null;
+ const existRenote: INote | null = null;
//#endregion
if (!existRenote) {
diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts
index 123c091c85..5b30bb5e15 100644
--- a/src/services/note/reaction/create.ts
+++ b/src/services/note/reaction/create.ts
@@ -36,7 +36,7 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise
res();
- const inc = {};
+ const inc: {[key: string]: number} = {};
inc[`reactionCounts.${reaction}`] = 1;
// Increment reactions count
diff --git a/src/text/html.ts b/src/text/html.ts
index b55d9b80a7..41adb2e7f7 100644
--- a/src/text/html.ts
+++ b/src/text/html.ts
@@ -1,7 +1,8 @@
-import { lib as emojilib } from 'emojilib';
+const { lib: emojilib } = require('emojilib');
import { JSDOM } from 'jsdom';
import config from '../config';
import { INote } from '../models/note';
+import { TextElement } from './parse';
const handlers: {[key: string]: (window: any, token: any, mentionedRemoteUsers: INote["mentionedRemoteUsers"]) => void} = {
bold({ document }, { bold }) {
@@ -90,7 +91,7 @@ const handlers: {[key: string]: (window: any, token: any, mentionedRemoteUsers:
}
};
-export default (tokens, mentionedRemoteUsers: INote['mentionedRemoteUsers'] = []) => {
+export default (tokens: TextElement[], mentionedRemoteUsers: INote['mentionedRemoteUsers'] = []) => {
const { window } = new JSDOM('');
for (const token of tokens) {
diff --git a/src/text/parse/core/syntax-highlighter.ts b/src/text/parse/core/syntax-highlighter.ts
index c0396b1fc6..3fb7a3b73d 100644
--- a/src/text/parse/core/syntax-highlighter.ts
+++ b/src/text/parse/core/syntax-highlighter.ts
@@ -1,4 +1,4 @@
-function escape(text) {
+function escape(text: string) {
return text
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;');
@@ -110,7 +110,14 @@ const symbols = [
'?'
];
-const elements = [
+type Token = {
+ html: string
+ next: number
+};
+
+type Element = (code: string, i: number, source: string) => (Token | null);
+
+const elements: Element[] = [
// comment
code => {
if (code.substr(0, 2) != '//') return null;
@@ -305,7 +312,7 @@ export default (source: string, lang?: string) => {
let i = 0;
- function push(token) {
+ function push(token: Token) {
html += token.html;
code = code.substr(token.next);
i += token.next;
diff --git a/src/text/parse/elements/bold.ts b/src/text/parse/elements/bold.ts
index ce25764457..0566ace8b7 100644
--- a/src/text/parse/elements/bold.ts
+++ b/src/text/parse/elements/bold.ts
@@ -2,7 +2,13 @@
* Bold
*/
-module.exports = text => {
+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];
@@ -10,5 +16,5 @@ module.exports = text => {
type: 'bold',
content: bold,
bold: bold.substr(2, bold.length - 4)
- };
-};
+ } as TextElementBold;
+}
diff --git a/src/text/parse/elements/code.ts b/src/text/parse/elements/code.ts
index 4821e95fe2..de87aa410b 100644
--- a/src/text/parse/elements/code.ts
+++ b/src/text/parse/elements/code.ts
@@ -4,7 +4,14 @@
import genHtml from '../core/syntax-highlighter';
-module.exports = text => {
+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];
@@ -13,5 +20,5 @@ module.exports = text => {
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/text/parse/elements/emoji.ts b/src/text/parse/elements/emoji.ts
index e24231a223..d0eed88965 100644
--- a/src/text/parse/elements/emoji.ts
+++ b/src/text/parse/elements/emoji.ts
@@ -2,7 +2,13 @@
* Emoji
*/
-module.exports = text => {
+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];
@@ -10,5 +16,5 @@ module.exports = text => {
type: 'emoji',
content: emoji,
emoji: emoji.substr(1, emoji.length - 2)
- };
-};
+ } as TextElementEmoji;
+}
diff --git a/src/text/parse/elements/hashtag.ts b/src/text/parse/elements/hashtag.ts
index ee57b140b8..cde0c2b224 100644
--- a/src/text/parse/elements/hashtag.ts
+++ b/src/text/parse/elements/hashtag.ts
@@ -2,7 +2,13 @@
* Hashtag
*/
-module.exports = (text, i) => {
+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];
@@ -15,5 +21,5 @@ module.exports = (text, i) => {
content: isHead ? hashtag : hashtag.substr(1),
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
});
- return res;
-};
+ return res as TextElementHashtag[];
+}
diff --git a/src/text/parse/elements/inline-code.ts b/src/text/parse/elements/inline-code.ts
index 9f9ef51a2b..bcb0bca0ad 100644
--- a/src/text/parse/elements/inline-code.ts
+++ b/src/text/parse/elements/inline-code.ts
@@ -4,7 +4,14 @@
import genHtml from '../core/syntax-highlighter';
-module.exports = text => {
+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];
@@ -13,5 +20,5 @@ module.exports = text => {
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/text/parse/elements/link.ts b/src/text/parse/elements/link.ts
index 35563ddc3d..7e0d6f5cf8 100644
--- a/src/text/parse/elements/link.ts
+++ b/src/text/parse/elements/link.ts
@@ -2,7 +2,15 @@
* Link
*/
-module.exports = text => {
+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] == '?';
@@ -15,5 +23,5 @@ module.exports = text => {
title: title,
url: url,
silent: silent
- };
-};
+ } as TextElementLink;
+}
diff --git a/src/text/parse/elements/mention.ts b/src/text/parse/elements/mention.ts
index 2ad2788300..a4140458d4 100644
--- a/src/text/parse/elements/mention.ts
+++ b/src/text/parse/elements/mention.ts
@@ -3,7 +3,14 @@
*/
import parseAcct from '../../../acct/parse';
-module.exports = text => {
+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];
@@ -13,5 +20,5 @@ module.exports = text => {
content: mention,
username,
host
- };
-};
+ } as TextElementMention;
+}
diff --git a/src/text/parse/elements/quote.ts b/src/text/parse/elements/quote.ts
index cc8cfffdc4..56de561f3f 100644
--- a/src/text/parse/elements/quote.ts
+++ b/src/text/parse/elements/quote.ts
@@ -2,7 +2,13 @@
* Quoted text
*/
-module.exports = 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];
@@ -10,5 +16,5 @@ module.exports = text => {
type: 'quote',
content: quote,
quote: quote.substr(1, quote.length - 2).trim(),
- };
-};
+ } as TextElementQuote;
+}
diff --git a/src/text/parse/elements/search.ts b/src/text/parse/elements/search.ts
index 12ee8ecbb8..4bd19ee3fa 100644
--- a/src/text/parse/elements/search.ts
+++ b/src/text/parse/elements/search.ts
@@ -2,7 +2,13 @@
* Search
*/
-module.exports = text => {
+export type TextElementSearch = {
+ type: "search"
+ content: string
+ query: string
+};
+
+export default function(text: string) {
const match = text.match(/^(.+?) 検索(\n|$)/);
if (!match) return null;
return {
@@ -10,4 +16,4 @@ module.exports = text => {
content: match[0],
query: match[1]
};
-};
+}
diff --git a/src/text/parse/elements/title.ts b/src/text/parse/elements/title.ts
index 9f4708f5d6..11b3abc61b 100644
--- a/src/text/parse/elements/title.ts
+++ b/src/text/parse/elements/title.ts
@@ -2,7 +2,13 @@
* Title
*/
-module.exports = text => {
+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];
@@ -10,5 +16,5 @@ module.exports = text => {
type: 'title',
content: title,
title: title.substr(1, title.length - 3)
- };
-};
+ } as TextElementTitle;
+}
diff --git a/src/text/parse/elements/url.ts b/src/text/parse/elements/url.ts
index 1003aff9c3..bbc27b4fd7 100644
--- a/src/text/parse/elements/url.ts
+++ b/src/text/parse/elements/url.ts
@@ -2,7 +2,13 @@
* URL
*/
-module.exports = text => {
+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];
@@ -10,5 +16,5 @@ module.exports = text => {
type: 'url',
content: url,
url: url
- };
-};
+ } as TextElementUrl;
+}
diff --git a/src/text/parse/index.ts b/src/text/parse/index.ts
index cfddd9f615..ccfef44591 100644
--- a/src/text/parse/index.ts
+++ b/src/text/parse/index.ts
@@ -2,6 +2,18 @@
* Misskey Text Analyzer
*/
+import { TextElementBold } from "./elements/bold";
+import { TextElementCode } from "./elements/code";
+import { TextElementEmoji } from "./elements/emoji";
+import { TextElementHashtag } from "./elements/hashtag";
+import { TextElementInlineCode } from "./elements/inline-code";
+import { TextElementLink } from "./elements/link";
+import { TextElementMention } from "./elements/mention";
+import { TextElementQuote } from "./elements/quote";
+import { TextElementSearch } from "./elements/search";
+import { TextElementTitle } from "./elements/title";
+import { TextElementUrl } from "./elements/url";
+
const elements = [
require('./elements/bold'),
require('./elements/title'),
@@ -14,17 +26,31 @@ const elements = [
require('./elements/quote'),
require('./elements/emoji'),
require('./elements/search')
-];
+].map(element => element.default as TextElementProcessor);
+
+export type TextElement = {type: "text", content: string}
+ | TextElementBold
+ | TextElementCode
+ | TextElementEmoji
+ | TextElementHashtag
+ | TextElementInlineCode
+ | TextElementLink
+ | TextElementMention
+ | TextElementQuote
+ | TextElementSearch
+ | TextElementTitle
+ | TextElementUrl;
+export type TextElementProcessor = (text: string, i: number) => TextElement | TextElement[];
-export default (source: string): any[] => {
+export default (source: string): TextElement[] => {
if (source == '') {
return null;
}
- const tokens = [];
+ const tokens: TextElement[] = [];
- function push(token) {
+ function push(token: TextElement) {
if (token != null) {
tokens.push(token);
source = source.substr(token.content.length);
@@ -59,9 +85,8 @@ export default (source: string): any[] => {
}
// テキストを纏める
- tokens[0] = [tokens[0]];
return tokens.reduce((a, b) => {
- if (a[a.length - 1].type == 'text' && b.type == 'text') {
+ if (a.length && a[a.length - 1].type == 'text' && b.type == 'text') {
const tail = a.pop();
return a.concat({
type: 'text',
@@ -70,5 +95,5 @@ export default (source: string): any[] => {
} else {
return a.concat(b);
}
- });
+ }, [] as TextElement[]);
};