diff options
Diffstat (limited to 'src/mfm')
| -rw-r--r-- | src/mfm/html-to-mfm.ts | 19 | ||||
| -rw-r--r-- | src/mfm/html.ts | 17 | ||||
| -rw-r--r-- | src/mfm/parse/core/syntax-highlighter.ts | 6 | ||||
| -rw-r--r-- | src/mfm/parse/elements/hashtag.ts | 4 |
4 files changed, 25 insertions, 21 deletions
diff --git a/src/mfm/html-to-mfm.ts b/src/mfm/html-to-mfm.ts index daa228ec51..6da1dbdad3 100644 --- a/src/mfm/html-to-mfm.ts +++ b/src/mfm/html-to-mfm.ts @@ -33,26 +33,27 @@ export default function(html: string): string { case 'a': const txt = getText(node); + const rel = node.attrs.find((x: any) => x.name == 'rel'); + const href = node.attrs.find((x: any) => x.name == 'href'); + // ハッシュタグ / hrefがない / txtがURL + if ((rel && rel.value.match('tag') !== null) || !href || href.value == txt) { + text += txt; // メンション - if (txt.startsWith('@')) { + } else if (txt.startsWith('@')) { const part = txt.split('@'); if (part.length == 2) { //#region ホスト名部分が省略されているので復元する - const href = new URL(node.attrs.find((x: any) => x.name == 'href').value); - const acct = txt + '@' + href.hostname; + const acct = `${txt}@${(new URL(href.value)).hostname}`; text += acct; - break; //#endregion } else if (part.length == 3) { text += txt; - break; } - } - - if (node.childNodes) { - node.childNodes.forEach((n: any) => analyze(n)); + // その他 + } else { + text += `[${txt}](${href.value})`; } break; diff --git a/src/mfm/html.ts b/src/mfm/html.ts index c798ee410a..df9959dc4b 100644 --- a/src/mfm/html.ts +++ b/src/mfm/html.ts @@ -4,10 +4,7 @@ const { JSDOM } = jsdom; import config from '../config'; import { INote } from '../models/note'; import { TextElement } from './parse'; - -function intersperse<T>(sep: T, xs: T[]): T[] { - return [].concat(...xs.map(x => [sep, x])).slice(1); -} +import { intersperse } from '../prelude/array'; const handlers: { [key: string]: (window: any, token: any, mentionedRemoteUsers: INote['mentionedRemoteUsers']) => void } = { bold({ document }, { bold }) { @@ -44,8 +41,8 @@ const handlers: { [key: string]: (window: any, token: any, mentionedRemoteUsers: hashtag({ document }, { hashtag }) { const a = document.createElement('a'); - a.href = config.url + '/tags/' + hashtag; - a.textContent = '#' + hashtag; + a.href = `${config.url}/tags/${hashtag}`; + a.textContent = `#${hashtag}`; a.setAttribute('rel', 'tag'); document.body.appendChild(a); }, @@ -85,8 +82,12 @@ const handlers: { [key: string]: (window: any, token: any, mentionedRemoteUsers: text({ document }, { content }) { const nodes = (content as string).split('\n').map(x => document.createTextNode(x)); - for (const x of intersperse(document.createElement('br'), nodes)) { - document.body.appendChild(x); + for (const x of intersperse('br', nodes)) { + if (x === 'br') { + document.body.appendChild(document.createElement('br')); + } else { + document.body.appendChild(x); + } } }, diff --git a/src/mfm/parse/core/syntax-highlighter.ts b/src/mfm/parse/core/syntax-highlighter.ts index 2b13608d2b..83aac89f1b 100644 --- a/src/mfm/parse/core/syntax-highlighter.ts +++ b/src/mfm/parse/core/syntax-highlighter.ts @@ -1,3 +1,5 @@ +import { capitalize, toUpperCase } from "../../../prelude/string"; + function escape(text: string) { return text .replace(/>/g, '>') @@ -89,8 +91,8 @@ const _keywords = [ ]; const keywords = _keywords - .concat(_keywords.map(k => k[0].toUpperCase() + k.substr(1))) - .concat(_keywords.map(k => k.toUpperCase())) + .concat(_keywords.map(capitalize)) + .concat(_keywords.map(toUpperCase)) .sort((a, b) => b.length - a.length); const symbols = [ diff --git a/src/mfm/parse/elements/hashtag.ts b/src/mfm/parse/elements/hashtag.ts index f4b6a78fa8..339026228a 100644 --- a/src/mfm/parse/elements/hashtag.ts +++ b/src/mfm/parse/elements/hashtag.ts @@ -9,9 +9,9 @@ export type TextElementHashtag = { }; export default function(text: string, i: number) { - if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null; + if (!(/^\s#[^\s\.,]+/.test(text) || (i == 0 && /^#[^\s\.,]+/.test(text)))) return null; const isHead = text.startsWith('#'); - const hashtag = text.match(/^\s?#[^\s]+/)[0]; + const hashtag = text.match(/^\s?#[^\s\.,]+/)[0]; const res: any[] = !isHead ? [{ type: 'text', content: text[0] |