/* * SPDX-FileCopyrightText: syuilo and misskey-project * SPDX-License-Identifier: AGPL-3.0-only */ import * as assert from 'assert'; import * as mfm from 'mfm-js'; import type { Config } from '@/config.js'; import { MfmService } from '@/core/MfmService.js'; describe('MfmService', () => { let config: Config; let mfmService: MfmService; beforeEach(() => { config = { url: 'https://example.com', } as unknown as Config; mfmService = new MfmService(config); }); describe('toHtml', () => { test('br', () => { const input = 'foo\nbar\nbaz'; const output = '
foo
bar
baz
foo
bar
baz
foo bar
'; assert.equal(mfmService.toHtml(mfm.parse(input)), output); }); test('escape', () => { const input = '```\nHello, world!
\n```'; const output = '<p>Hello, world!</p>';
assert.equal(mfmService.toHtml(mfm.parse(input)), output);
});
test('ruby', () => {
const input = '$[ruby some text ignore me]';
const output = 'some
'; assert.equal(mfmService.toHtml(mfm.parse(input)), output); }); test('ruby2', () => { const input = '$[ruby *some text* ignore me]'; const output = 'some text
'; assert.equal(mfmService.toHtml(mfm.parse(input)), output); }); test('ruby 3', () => { const input = '$[ruby $[group *some* text] ignore me]'; const output = 'some text
'; assert.equal(mfmService.toHtml(mfm.parse(input)), output); }); test('inline', () => { const input = 'https://example.com'; const output = 'https://example.com'; assert.equal(mfmService.toHtml(mfm.parse(input), [], [], true), output); }); }); describe('toMastoApiHtml', () => { test('br', () => { const input = 'foo\nbar\nbaz'; const output = 'foo
bar
baz
foo
bar
baz
Hello, world!
\n```'; const output = '<p>Hello, world!</p>';
assert.equal(mfmService.toMastoApiHtml(mfm.parse(input)), output);
});
test('ruby', async () => {
const input = '$[ruby $[group *some* text] ignore me]';
const output = '*some* text
'; assert.equal(await mfmService.toMastoApiHtml(mfm.parse(input)), output); }); }); describe('fromHtml', () => { test('p', () => { assert.deepStrictEqual(mfmService.fromHtml('a
b
'), 'a\n\nb'); }); test('block element', () => { assert.deepStrictEqual(mfmService.fromHtml('a\nb'), '```\na\nb\n```');
});
test('inline code', () => {
assert.deepStrictEqual(mfmService.fromHtml('a'), '`a`');
});
test('quote', () => {
assert.deepStrictEqual(mfmService.fromHtml('a\nb'), '> a\n> b'); }); test('br', () => { assert.deepStrictEqual(mfmService.fromHtml('
abc
d
a c d
'), 'a [c](https://example.com/b) d'); }); test('link with different text, but not encoded', () => { assert.deepStrictEqual(mfmService.fromHtml('a c d
'), 'a [c](a c d
'), 'a [c](b) d'); }); test('link without href', () => { assert.deepStrictEqual(mfmService.fromHtml('a c d
'), 'a c d'); }); test('link without text', () => { assert.deepStrictEqual(mfmService.fromHtml(''), 'a https://example.com/b d'); }); test('link without both', () => { assert.deepStrictEqual(mfmService.fromHtml(''), 'a d'); }); test('ruby', () => { assert.deepStrictEqual(mfmService.fromHtml('a Misskey b
'), 'a $[ruby Misskey ミスキー] b'); assert.deepStrictEqual(mfmService.fromHtml('a MisskeyMisskey b
'), 'a $[ruby Misskey ミスキー]$[ruby Misskey ミスキー] b'); }); test('ruby with spaces', () => { assert.deepStrictEqual(mfmService.fromHtml('a Miss key b c
'), 'a $[ruby $[group Miss key] ミスキー] b c'); assert.deepStrictEqual(mfmService.fromHtml('a Misskey b c
'), 'a $[ruby $[group Misskey] ミス キー] b c'); assert.deepStrictEqual( mfmService.fromHtml('a MisskeyMisskeyMisskey b
'), 'a $[ruby Misskey ミスキー]$[ruby $[group Misskey] ミス キー]$[ruby Misskey ミスキー] b', ); }); test('ruby with other inline tags', () => { assert.deepStrictEqual(mfmService.fromHtml('a Misskey b c
'), 'a $[ruby **Misskey** ミスキー] b c'); }); test('mention', () => { assert.deepStrictEqual(mfmService.fromHtml('a @user d
'), 'a @user@example.com d'); }); test('hashtag', () => { assert.deepStrictEqual(mfmService.fromHtml('a #a d
', ['#a']), 'a #a d'); }); test('ruby', () => { assert.deepStrictEqual( mfmService.fromHtml(' some text and '), '$[ruby $[group some text ] ignore me]$[ruby $[group and ] more]', ); }); }); });