Skip to content

Commit

Permalink
recommendSuffixSpace, recommendSuffixNewLine
Browse files Browse the repository at this point in the history
  • Loading branch information
taiyme committed Feb 22, 2024
1 parent 9a00745 commit 7b61fb4
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions packages/frontend/src/scripts/tms/numberquote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import * as Misskey from 'misskey-js';
import * as mfm from 'mfm-js';
import { misskeyApi } from '@/scripts/misskey-api.js';
import { NoteEntityOrId, NoteParameters, toParameters } from '@/scripts/tms/to-parameters.js';

Expand All @@ -13,20 +14,33 @@ export const numberquote = async (noteEntityOrId: NoteEntityOrId, fromId?: strin
return misskeyApi('notes/create', parameters, me.token);
};

const incrText = (text: NoteParameters['text']): NoteParameters['text'] => {
const token = extractNqTokens(text);
if (token.num === '') return `${token.str}2`;
return `${token.str}${(BigInt(token.num) + 1n).toString(10)}`;
};

const decrText = (text: NoteParameters['text']): NoteParameters['text'] => {
const token = extractNqTokens(text);
if (token.num === '') return `${token.str}0`;
return `${token.str}${(BigInt(token.num) - 1n).toString(10)}`;
};

const extractNqTokens = (text: NoteParameters['text']): {
readonly str: string;
readonly num: string;
} => {
if (text == null || text === '') {
return { str: '', num: '' } as const;
}
if (text.endsWith('</center>')) {
const str = `${text}\n`;
const ast = mfm.parse(text);
const lastNode = ast.at(-1) ?? null;
if (recommendSuffixSpace(lastNode)) {
const str = `${text} `;
return { str, num: '' } as const;
}
if (text.endsWith(':')) {
const ZWSP = '\u200b';
const str = `${text}${ZWSP}`;
if (recommendSuffixNewLine(lastNode)) {
const str = `${text}\n`;
return { str, num: '' } as const;
}
const result = text.split(/(\-?\d+)$/);
Expand All @@ -35,14 +49,14 @@ const extractNqTokens = (text: NoteParameters['text']): {
return { str, num } as const;
};

const incrText = (text: NoteParameters['text']): NoteParameters['text'] => {
const token = extractNqTokens(text);
if (token.num === '') return `${token.str}2`;
return `${token.str}${(BigInt(token.num) + 1n).toString(10)}`;
const recommendSuffixSpace = (node: mfm.MfmNode | null): boolean => {
if (node == null) return false;
if (node.type === 'url' && !node.props.brackets) return true;
if (node.type === 'text' && node.props.text.endsWith('-')) return true;
return ['mention', 'hashtag', 'emojiCode'].includes(node.type);
};

const decrText = (text: NoteParameters['text']): NoteParameters['text'] => {
const token = extractNqTokens(text);
if (token.num === '') return `${token.str}0`;
return `${token.str}${(BigInt(token.num) - 1n).toString(10)}`;
const recommendSuffixNewLine = (node: mfm.MfmNode | null): boolean => {
if (node == null) return false;
return ['quote', 'search', 'blockCode', 'mathBlock', 'center'].includes(node.type);
};

0 comments on commit 7b61fb4

Please sign in to comment.