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

Commit859d8e5

Browse files
committed
refactor parse regexes
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent72430c6 commit859d8e5

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

‎src/utils/parse.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ type TutorialFrame = {
77
levels:T.Level[];
88
};
99

10+
constR={
11+
summary:/^#\s(?<tutorialTitle>.*)[\n\r]+(?<tutorialDescription>[^]*)/,
12+
level:/^(#{2}\s(?<levelId>L?\d+\.?)\s(?<levelTitle>.*)[\n\r]*(>\s(?<levelSummary>.*))?[\n\r]+(?<levelContent>[^]*))/,
13+
step:/^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/,
14+
hints:/^(#{4}\sHINTS[\n\r]+([\*|\-]\s(?<hintContent>[^]*))[\n\r]+)+/,
15+
subtasks:/^(#{4}\sSUBTASKS[\n\r]+([\*|\-]\s(?<subtaskContent>[^]*))[\n\r]+)+/,
16+
listItem:/[\n\r]+[\*|\-]\s/,
17+
};
18+
1019
exportfunctionparseMdContent(md:string):TutorialFrame|never{
1120
letstart:number=-1;
1221
constparts:any[]=[];
@@ -34,9 +43,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
3443
};
3544

3645
// Capture summary
37-
constsummaryMatch=parts
38-
.shift()
39-
.match(/^#\s(?<tutorialTitle>.*)[\n\r]+(?<tutorialDescription>[^]*)/);
46+
constsummaryMatch=parts.shift().match(R.summary);
4047
if(summaryMatch.groups.tutorialTitle){
4148
mdContent.summary.title=summaryMatch.groups.tutorialTitle.trim();
4249
}
@@ -49,8 +56,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
4956
// Identify each part of the content
5057
parts.forEach((section:string)=>{
5158
// match level
52-
constlevelRegex=/^(#{2}\s(?<levelId>L?\d+\.?)\s(?<levelTitle>.*)[\n\r]*(>\s(?<levelSummary>.*))?[\n\r]+(?<levelContent>[^]*))/;
53-
constlevelMatch:RegExpMatchArray|null=section.match(levelRegex);
59+
constlevelMatch:RegExpMatchArray|null=section.match(R.level);
5460

5561
if(levelMatch&&levelMatch.groups){
5662
constlevelId=levelMatch.groups.levelId.replace(".","");
@@ -77,8 +83,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
7783
};
7884
}else{
7985
// match step
80-
conststepRegex=/^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
81-
conststepMatch:RegExpMatchArray|null=section.match(stepRegex);
86+
conststepMatch:RegExpMatchArray|null=section.match(R.step);
8287
if(stepMatch&&stepMatch.groups){
8388
current={
8489
levelId:current.levelId,
@@ -91,17 +96,14 @@ export function parseMdContent(md: string): TutorialFrame | never {
9196
content:stepContent.trim(),
9297
};
9398
}else{
94-
consthintDetectRegex=/^(#{4}\sHINTS[\n\r]+([\*|\-]\s(?<hintContent>[^]*))[\n\r]+)+/;
95-
consthintMatch=section.match(hintDetectRegex);
96-
constsubtaskDetectRegex=/^(#{4}\sSUBTASKS[\n\r]+([\*|\-]\s(?<subtaskContent>[^]*))[\n\r]+)+/;
97-
constsubtaskMatch=section.match(subtaskDetectRegex);
98-
constlistItemregex=/[\n\r]+[\*|\-]\s/;
99+
consthintMatch=section.match(R.hints);
100+
constsubtaskMatch=section.match(R.subtasks);
99101

100102
switch(true){
101103
// parse hints from stepContent
102104
case!!hintMatch:
103105
consthints=section
104-
.split(listItemregex)
106+
.split(R.listItem)
105107
.slice(1)// remove #### HINTS
106108
.map((h)=>h.trim());
107109
if(hints.length){
@@ -113,7 +115,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
113115
// parse subtasks from stepContent
114116
case!!subtaskMatch:
115117
constsubtasks=section
116-
.split(listItemregex)
118+
.split(R.listItem)
117119
.slice(1)// remove #### SUBTASKS
118120
.map((h)=>h.trim());
119121
if(subtasks.length){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp