mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-26 23:03:09 +02:00
Merge pull request #1738 from rinsuki/features/ts-noimplicitany-true
[WIP] noImplicitAny: true
This commit is contained in:
commit
a766faeae9
28 changed files with 260 additions and 101 deletions
|
@ -8,12 +8,12 @@ import * as gutil from 'gulp-util';
|
|||
import * as ts from 'gulp-typescript';
|
||||
const sourcemaps = require('gulp-sourcemaps');
|
||||
import tslint from 'gulp-tslint';
|
||||
import cssnano = require('gulp-cssnano');
|
||||
const cssnano = require('gulp-cssnano');
|
||||
import * as uglifyComposer from 'gulp-uglify/composer';
|
||||
import pug = require('gulp-pug');
|
||||
import * as rimraf from 'rimraf';
|
||||
import chalk from 'chalk';
|
||||
import imagemin = require('gulp-imagemin');
|
||||
const imagemin = require('gulp-imagemin');
|
||||
import * as rename from 'gulp-rename';
|
||||
import * as mocha from 'gulp-mocha';
|
||||
import * as replace from 'gulp-replace';
|
||||
|
|
|
@ -5,12 +5,16 @@
|
|||
import * as fs from 'fs';
|
||||
import * as yaml from 'js-yaml';
|
||||
|
||||
const loadLang = lang => yaml.safeLoad(
|
||||
fs.readFileSync(`./locales/${lang}.yml`, 'utf-8'));
|
||||
export type LangKey = 'de' | 'en' | 'fr' | 'ja' | 'pl';
|
||||
export type LocaleObjectChildren = LocaleObject | string | undefined;
|
||||
export type LocaleObject = {[key: string]: LocaleObjectChildren };
|
||||
|
||||
const loadLang = (lang: LangKey) => yaml.safeLoad(
|
||||
fs.readFileSync(`./locales/${lang}.yml`, 'utf-8')) as LocaleObject;
|
||||
|
||||
const native = loadLang('ja');
|
||||
|
||||
const langs = {
|
||||
const langs: {[key in LangKey]: LocaleObject} = {
|
||||
'de': loadLang('de'),
|
||||
'en': loadLang('en'),
|
||||
'fr': loadLang('fr'),
|
||||
|
@ -23,4 +27,8 @@ Object.entries(langs).map(([, locale]) => {
|
|||
locale = Object.assign({}, native, locale);
|
||||
});
|
||||
|
||||
export function isAvailableLanguage(lang: string): lang is LangKey {
|
||||
return lang in langs;
|
||||
}
|
||||
|
||||
export default langs;
|
||||
|
|
12
package.json
12
package.json
|
@ -23,10 +23,10 @@
|
|||
"format": "gulp format"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fortawesome/fontawesome": "1.0.1",
|
||||
"@fortawesome/fontawesome-free-brands": "5.0.2",
|
||||
"@fortawesome/fontawesome-free-regular": "5.0.2",
|
||||
"@fortawesome/fontawesome-free-solid": "5.0.2",
|
||||
"@fortawesome/fontawesome": "1.1.8",
|
||||
"@fortawesome/fontawesome-free-brands": "5.0.13",
|
||||
"@fortawesome/fontawesome-free-regular": "5.0.13",
|
||||
"@fortawesome/fontawesome-free-solid": "5.0.13",
|
||||
"@koa/cors": "2.2.1",
|
||||
"@prezzemolo/rap": "0.1.2",
|
||||
"@prezzemolo/zip": "0.0.3",
|
||||
|
@ -216,5 +216,9 @@
|
|||
"websocket": "1.0.26",
|
||||
"ws": "5.2.0",
|
||||
"xev": "2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/file-type": "5.2.1",
|
||||
"@types/jsdom": "11.0.5"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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`);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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 => {
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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>> = [];
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
function escape(text) {
|
||||
function escape(text: string) {
|
||||
return text
|
||||
.replace(/>/g, '>')
|
||||
.replace(/</g, '<');
|
||||
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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[];
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 default (source: string): any[] => {
|
||||
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): 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[]);
|
||||
};
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"noEmitOnError": false,
|
||||
"noImplicitAny": false,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedParameters": false,
|
||||
"noUnusedLocals": true,
|
||||
|
|
57
yarn.lock
57
yarn.lock
|
@ -2,21 +2,33 @@
|
|||
# yarn lockfile v1
|
||||
|
||||
|
||||
"@fortawesome/fontawesome-free-brands@5.0.2":
|
||||
version "5.0.2"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-brands/-/fontawesome-free-brands-5.0.2.tgz#a1cc602eec40a379a3dd8a44c78b31110dd3d3d3"
|
||||
"@fortawesome/fontawesome-common-types@^0.1.7":
|
||||
version "0.1.7"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.1.7.tgz#4336c4b06d0b5608ff1215464b66fcf9f4795284"
|
||||
|
||||
"@fortawesome/fontawesome-free-regular@5.0.2":
|
||||
version "5.0.2"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-regular/-/fontawesome-free-regular-5.0.2.tgz#429af86bed14689f87648e6322983c65c782c017"
|
||||
"@fortawesome/fontawesome-free-brands@5.0.13":
|
||||
version "5.0.13"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-brands/-/fontawesome-free-brands-5.0.13.tgz#4d15ff4e1e862d5e4a4df3654f8e8acbd47e9c09"
|
||||
dependencies:
|
||||
"@fortawesome/fontawesome-common-types" "^0.1.7"
|
||||
|
||||
"@fortawesome/fontawesome-free-solid@5.0.2":
|
||||
version "5.0.2"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-solid/-/fontawesome-free-solid-5.0.2.tgz#090ce2c59dd5ec76983f3da8a43e1ab0321b42d5"
|
||||
"@fortawesome/fontawesome-free-regular@5.0.13":
|
||||
version "5.0.13"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-regular/-/fontawesome-free-regular-5.0.13.tgz#eb78c30184e3f456a423a1dcfa0f682f7b50de4a"
|
||||
dependencies:
|
||||
"@fortawesome/fontawesome-common-types" "^0.1.7"
|
||||
|
||||
"@fortawesome/fontawesome@1.0.1":
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome/-/fontawesome-1.0.1.tgz#8ac60e1e7b437889baf9c9d6e3a61ef3b637170d"
|
||||
"@fortawesome/fontawesome-free-solid@5.0.13":
|
||||
version "5.0.13"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-solid/-/fontawesome-free-solid-5.0.13.tgz#24b61aaf471a9d34a5364b052d64a516285ba894"
|
||||
dependencies:
|
||||
"@fortawesome/fontawesome-common-types" "^0.1.7"
|
||||
|
||||
"@fortawesome/fontawesome@1.1.8":
|
||||
version "1.1.8"
|
||||
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome/-/fontawesome-1.1.8.tgz#75fe66a60f95508160bb16bd781ad7d89b280f5b"
|
||||
dependencies:
|
||||
"@fortawesome/fontawesome-common-types" "^0.1.7"
|
||||
|
||||
"@gulp-sourcemaps/identity-map@1.X":
|
||||
version "1.0.1"
|
||||
|
@ -164,6 +176,12 @@
|
|||
version "1.3.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/fancy-log/-/fancy-log-1.3.0.tgz#a61ab476e5e628cd07a846330df53b85e05c8ce0"
|
||||
|
||||
"@types/file-type@5.2.1":
|
||||
version "5.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/file-type/-/file-type-5.2.1.tgz#e7af49e08187b6b7598509c5e416669d25fa3461"
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/form-data@*":
|
||||
version "2.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.2.1.tgz#ee2b3b8eaa11c0938289953606b745b738c54b1e"
|
||||
|
@ -265,6 +283,15 @@
|
|||
version "3.11.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.11.1.tgz#ac5bab26be5f9c6f74b6b23420f2cfa5a7a6ba40"
|
||||
|
||||
"@types/jsdom@11.0.5":
|
||||
version "11.0.5"
|
||||
resolved "https://registry.yarnpkg.com/@types/jsdom/-/jsdom-11.0.5.tgz#b12fffc73eb3731b218e9665a50f023b6b84b5cb"
|
||||
dependencies:
|
||||
"@types/events" "*"
|
||||
"@types/node" "*"
|
||||
"@types/tough-cookie" "*"
|
||||
parse5 "^3.0.2"
|
||||
|
||||
"@types/keygrip@*":
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.1.tgz#ff540462d2fb4d0a88441ceaf27d287b01c3d878"
|
||||
|
@ -8209,6 +8236,12 @@ parse5@4.0.0:
|
|||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"
|
||||
|
||||
parse5@^3.0.2:
|
||||
version "3.0.3"
|
||||
resolved "https://registry.yarnpkg.com/parse5/-/parse5-3.0.3.tgz#042f792ffdd36851551cf4e9e066b3874ab45b5c"
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
parseurl@^1.3.0, parseurl@~1.3.2:
|
||||
version "1.3.2"
|
||||
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.2.tgz#fc289d4ed8993119460c156253262cdc8de65bf3"
|
||||
|
|
Loading…
Reference in a new issue