Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: silent url #141

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
### Bugfixes

-->
## 0.24.1

### Features
- URL node with silence with `?<url>`

### Improvements

### Changes

### Bugfixes

## 0.24.0
### Features
Expand Down
3 changes: 2 additions & 1 deletion etc/mfm-js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,13 @@ export type MfmUrl = {
props: {
url: string;
brackets?: boolean;
silent?: boolean;
};
children?: [];
};

// @public (undocumented)
export const N_URL: (value: string, brackets?: boolean) => NodeType<'url'>;
export const N_URL: (value: string, brackets?: boolean, silent?: boolean) => NodeType<'url'>;

// @public (undocumented)
export type NodeType<T extends MfmNode['type']> = T extends 'quote' ? MfmQuote : T extends 'search' ? MfmSearch : T extends 'blockCode' ? MfmCodeBlock : T extends 'mathBlock' ? MfmMathBlock : T extends 'center' ? MfmCenter : T extends 'unicodeEmoji' ? MfmUnicodeEmoji : T extends 'emojiCode' ? MfmEmojiCode : T extends 'bold' ? MfmBold : T extends 'small' ? MfmSmall : T extends 'italic' ? MfmItalic : T extends 'strike' ? MfmStrike : T extends 'inlineCode' ? MfmInlineCode : T extends 'mathInline' ? MfmMathInline : T extends 'mention' ? MfmMention : T extends 'hashtag' ? MfmHashtag : T extends 'url' ? MfmUrl : T extends 'link' ? MfmLink : T extends 'fn' ? MfmFn : T extends 'plain' ? MfmPlain : T extends 'text' ? MfmText : never;
Expand Down
7 changes: 4 additions & 3 deletions src/internal/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// tsdのテストでファイルを追加しているにも関わらず「@twemoji/parser/dist/lib/regex」の型定義ファイルがないとエラーが出るため、
// このエラーを無視する。
/* eslint @typescript-eslint/ban-ts-comment: 1 */
// @ts-ignore

Check warning on line 10 in src/internal/parser.ts

View workflow job for this annotation

GitHub Actions / lint

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
import twemojiRegex from '@twemoji/parser/dist/lib/regex';

type ArgPair = { k: string, v: string | true };
Expand Down Expand Up @@ -746,7 +746,7 @@
},

urlAlt: () => {
const open = P.str('<');
const open = P.alt([P.str('?<'), P.str('<')]);
const close = P.str('>');
const parser = P.seq(
notLinkLabel,
Expand All @@ -760,8 +760,9 @@
if (!result.success) {
return P.failure();
}
const text = result.value.slice(1, (result.value.length - 1));
return P.success(result.index, M.N_URL(text, true));
const silent = result.value.startsWith('?');
const text = silent ? result.value.slice(2, (result.value.length - 1)) : result.value.slice(1, (result.value.length - 1));
return P.success(result.index, M.N_URL(text, true, silent));
});
},

Expand Down
4 changes: 3 additions & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ export type MfmUrl = {
props: {
url: string;
brackets?: boolean;
silent?: boolean;
};
children?: [];
};
export const N_URL = (value: string, brackets?: boolean): NodeType<'url'> => {
export const N_URL = (value: string, brackets?: boolean, silent?: boolean): NodeType<'url'> => {
const node: MfmUrl = { type: 'url', props: { url: value } };
if (brackets) node.props.brackets = brackets;
if (silent) node.props.silent = silent;
return node;
};

Expand Down
16 changes: 16 additions & 0 deletions test/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,22 @@ hoge`;
assert.deepStrictEqual(mfm.parse(input), output);
});

test('match non-ascii characters contained url with angle brackets with silent', () => {
const input = '?<https://大石泉すき.example.com>';
const output = [
N_URL('https://大石泉すき.example.com', true, true),
];
assert.deepStrictEqual(mfm.parse(input), output);
});

test('match ascii characters contained url with angle brackets with silent', () => {
const input = '?<https://example.com>';
const output = [
N_URL('https://example.com', true, true),
];
assert.deepStrictEqual(mfm.parse(input), output);
});

test('prevent xss', () => {
const input = 'javascript:foo';
const output = [
Expand Down
Loading