Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9163dca

Browse files
committed
feat: 🎸 add spoiler tags
1 parent16f6370 commit9163dca

File tree

7 files changed

+115
-1
lines changed

7 files changed

+115
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"type":"root",
3+
"children": [
4+
{
5+
"type":"paragraph",
6+
"len":121,
7+
"children": [
8+
{
9+
"type":"image",
10+
"len":120,
11+
"url":"https://user-images.githubusercontent.com/9773803/53509104-6fc53000-3abb-11e9-8ad3-71882cb9f8d3.png",
12+
"alt":"",
13+
"title":"this is title"
14+
}
15+
]
16+
}
17+
],
18+
"len":122
19+
}

‎fixtures/md/image-with-title.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
![](https://user-images.githubusercontent.com/9773803/53509104-6fc53000-3abb-11e9-8ad3-71882cb9f8d3.png"this is title")

‎src/__tests__/inline.spec.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,78 @@ describe('Inline Markdown', () => {
374374
});
375375
});
376376

377+
describe('spoiler text',()=>{
378+
test('works when spoiler text is the only token',()=>{
379+
constparser=create();
380+
constast=parser.tokenizeInline('~~~foobar~~~');
381+
expect(ast).toMatchObject([
382+
{
383+
type:'spoiler',
384+
children:[
385+
{
386+
type:'text',
387+
value:'foobar',
388+
},
389+
],
390+
},
391+
]);
392+
});
393+
394+
test('works when inside other inline text',()=>{
395+
constparser=create();
396+
constast=parser.tokenizeInline('hello ~~~ foobar ~~~ world');
397+
expect(ast).toMatchObject([
398+
{type:'text',value:'hello '},
399+
{
400+
type:'spoiler',
401+
children:[
402+
{
403+
type:'text',
404+
value:' foobar ',
405+
},
406+
],
407+
},
408+
{type:'text',value:' world'},
409+
]);
410+
});
411+
412+
test('works when surrounded by deleted text',()=>{
413+
constparser=create();
414+
constast=parser.tokenizeInline('~~hello~~ ~~~ foobar ~~~ ~~world~~');
415+
expect(ast).toMatchObject([
416+
{
417+
type:'delete',
418+
children:[
419+
{
420+
type:'text',
421+
value:'hello',
422+
},
423+
],
424+
},
425+
{type:'text',value:' '},
426+
{
427+
type:'spoiler',
428+
children:[
429+
{
430+
type:'text',
431+
value:' foobar ',
432+
},
433+
],
434+
},
435+
{type:'text',value:' '},
436+
{
437+
type:'delete',
438+
children:[
439+
{
440+
type:'text',
441+
value:'world',
442+
},
443+
],
444+
},
445+
]);
446+
});
447+
});
448+
377449
describe('math',()=>{
378450
test('works',()=>{
379451
constparser=create();

‎src/presets/defaults.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import emphasis from '../tokenizer/emphasis';
88
import{TTokenizer,TBlockToken,TInlineToken}from'../types';
99
importstrongfrom'../tokenizer/strong';
1010
importdeletedTextfrom'../tokenizer/delete';
11+
importspoilerfrom'../tokenizer/spoiler';
1112
importinlineMathfrom'../tokenizer/inlineMath';
1213
importfootnoteReferencefrom'../tokenizer/footnoteReference';
1314
importreferencefrom'../tokenizer/reference';
@@ -52,6 +53,7 @@ const preset = {
5253
inlineCode(),
5354
strong,
5455
emphasis,
56+
spoiler,
5557
deletedText(),
5658
inlineMath(),
5759
footnoteReference,

‎src/tokenizer/spoiler.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import{TTokenizer,ISpoiler}from'../types';
2+
3+
constREG=/^~~~([\s\S]*)~~~/;
4+
5+
// tslint:disable only-arrow-functions, no-invalid-this
6+
constspoiler:TTokenizer<ISpoiler>=function(eat,value){
7+
constmatches=value.match(REG);
8+
returnmatches ?eat(matches[0],'spoiler',this.tokenizeInline(matches[1])) :void0;
9+
};
10+
11+
exportdefaultspoiler;

‎src/tokenizer/text.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import{urlInline}from'../regex';
22
import{TTokenizer,IText}from'../types';
33

4-
constREG=newRegExp('^[\\s\\S]+?(?=[\\<!\\[_*`:~#@\\$\\^=\\+]| {2,}\\n|('+urlInline.source+')|\\\\n|\\\\`|$)');
4+
constREG=newRegExp(
5+
'^[\\s\\S]+?(?=[\\<!\\[_*`:~\\|#@\\$\\^=\\+]| {2,}\\n|('+urlInline.source+')|\\\\n|\\\\`|$)'
6+
);
57

68
consttext=()=>{
79
// tslint:disable only-arrow-functions

‎src/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type TTokenTypeInline =
2121
|'strong'
2222
|'emphasis'
2323
|'delete'
24+
|'spoiler'
2425
|'inlineMath'
2526
|'footnoteReference'
2627
|'linkReference'
@@ -154,6 +155,10 @@ export interface IDelete extends IToken {
154155
type:'delete';
155156
}
156157

158+
exportinterfaceISpoilerextendsIToken{
159+
type:'spoiler';
160+
}
161+
157162
exportinterfaceIInlineMathextendsIToken{
158163
type:'inlineMath';
159164
}
@@ -257,6 +262,7 @@ export type TInlineToken =
257262
|IStrong
258263
|IEmphasis
259264
|IDelete
265+
|ISpoiler
260266
|IInlineMath
261267
|IFootnoteReference
262268
|ILinkReference

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp