Skip to content

Commit

Permalink
nyaizeをテキストのみに適用するように
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Mar 22, 2021
1 parent 81a8ed1 commit 6f3c7db
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
117 changes: 117 additions & 0 deletions src/mfm/to-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { MfmForest, MfmTree } from './prelude';
import { nyaize } from '../misc/nyaize';

export type RestoreOptions = {
doNyaize?: boolean;
};

export function toString(tokens: MfmForest | null, opts?: RestoreOptions): string {
if (tokens === null) return '';

function appendChildren(children: MfmForest, opts?: RestoreOptions): string {
return children.map(t => handlers[t.node.type](t, opts)).join('');
}

const handlers: { [key: string]: (token: MfmTree, opts?: RestoreOptions) => string } = {
bold(token, opts) {
return `**${appendChildren(token.children, opts)}**`;
},

big(token, opts) {
return `***${appendChildren(token.children, opts)}***`;
},

small(token, opts) {
return `<small>${appendChildren(token.children, opts)}</small>`;
},

strike(token, opts) {
return `~~${appendChildren(token.children, opts)}~~`;
},

italic(token, opts) {
return `<i>${appendChildren(token.children, opts)}</i>`;
},

motion(token, opts) {
return `<motion>${appendChildren(token.children, opts)}</motion>`;
},

spin(token, opts) {
const attr = token.node.props?.attr;
const post = attr ? ` ${attr}` : '';
return `<spin${post}>${appendChildren(token.children, opts)}</spin>`;
},

jump(token, opts) {
return `<jump>${appendChildren(token.children, opts)}</jump>`;
},

flip(token, opts) {
return `<flip>${appendChildren(token.children, opts)}</flip>`;
},

blockCode(token) {
return `\`\`\`${token.node.props.lang || ''}\n${token.node.props.code}\n\`\`\`\n`;
},

center(token, opts) {
return `<center>${appendChildren(token.children, opts)}</center>`;
},

emoji(token) {
return (token.node.props.emoji ? token.node.props.emoji : `:${token.node.props.name}:`);
},

hashtag(token) {
return `#${token.node.props.hashtag}`;
},

inlineCode(token) {
return `\`${token.node.props.code}\``;
},

mathInline(token) {
return `\\(${token.node.props.formula}\\)`;
},

mathBlock(token) {
return `\\[${token.node.props.formula}\\]`;
},

link(token, opts) {
if (token.node.props.silent) {
return `?[${appendChildren(token.children, opts)}](${token.node.props.url})`;
} else {
return `[${appendChildren(token.children, opts)}](${token.node.props.url})`;
}
},

mention(token) {
return token.node.props.canonical;
},

quote(token) {
return `${appendChildren(token.children, {doNyaize: false}).replace(/^/gm, '>').trim()}\n`;
},

title(token, opts) {
return `[${appendChildren(token.children, opts)}]\n`;
},

text(token, opts) {
return (opts && opts.doNyaize) ? nyaize(token.node.props.text) : token.node.props.text;
},

url(token) {
return `<${token.node.props.url}>`;
},

search(token, opts) {
const query = token.node.props.query;
return `${(opts && opts.doNyaize ? nyaize(query) : query)} [search]\n`;
}
};

return appendChildren(tokens, { doNyaize: (opts && opts.doNyaize) || false }).trim();
}
5 changes: 4 additions & 1 deletion src/models/repositories/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { SchemaType } from '../../misc/schema';
import { awaitAll } from '../../prelude/await-all';
import { Emoji } from '../entities/emoji';
import { decodeReaction, convertLegacyReactions, convertLegacyReaction } from '../../misc/reaction-lib';
import { parse } from '../../mfm/parse';
import { toString } from '../../mfm/to-string';

export type PackedNote = SchemaType<typeof packedNoteSchema>;

Expand Down Expand Up @@ -250,7 +252,8 @@ export class NoteRepository extends Repository<Note> {
});

if (packed.user.isCat && packed.text) {
packed.text = nyaize(packed.text);
const tokens = packed.text ? parse(packed.text) : [];
packed.text = toString(tokens, { doNyaize: true });
}

if (!opts.skipHide) {
Expand Down

0 comments on commit 6f3c7db

Please sign in to comment.