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

Feature/subtasks#57

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ShMcK merged 3 commits intomasterfromfeature/subtasks
Jul 5, 2020
Merged
Changes from1 commit
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
PrevPrevious commit
refactor parse regexes
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
@ShMcK
ShMcK committedJul 5, 2020
commit859d8e526706cce0666dae9a42012113c8747d9d
30 changes: 16 additions & 14 deletionssrc/utils/parse.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,15 @@ type TutorialFrame = {
levels: T.Level[];
};

const R = {
summary: /^#\s(?<tutorialTitle>.*)[\n\r]+(?<tutorialDescription>[^]*)/,
level: /^(#{2}\s(?<levelId>L?\d+\.?)\s(?<levelTitle>.*)[\n\r]*(>\s(?<levelSummary>.*))?[\n\r]+(?<levelContent>[^]*))/,
step: /^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/,
hints: /^(#{4}\sHINTS[\n\r]+([\*|\-]\s(?<hintContent>[^]*))[\n\r]+)+/,
subtasks: /^(#{4}\sSUBTASKS[\n\r]+([\*|\-]\s(?<subtaskContent>[^]*))[\n\r]+)+/,
listItem: /[\n\r]+[\*|\-]\s/,
};

export function parseMdContent(md: string): TutorialFrame | never {
let start: number = -1;
const parts: any[] = [];
Expand DownExpand Up@@ -34,9 +43,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
};

// Capture summary
const summaryMatch = parts
.shift()
.match(/^#\s(?<tutorialTitle>.*)[\n\r]+(?<tutorialDescription>[^]*)/);
const summaryMatch = parts.shift().match(R.summary);
if (summaryMatch.groups.tutorialTitle) {
mdContent.summary.title = summaryMatch.groups.tutorialTitle.trim();
}
Expand All@@ -49,8 +56,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
// Identify each part of the content
parts.forEach((section: string) => {
// match level
const levelRegex = /^(#{2}\s(?<levelId>L?\d+\.?)\s(?<levelTitle>.*)[\n\r]*(>\s(?<levelSummary>.*))?[\n\r]+(?<levelContent>[^]*))/;
const levelMatch: RegExpMatchArray | null = section.match(levelRegex);
const levelMatch: RegExpMatchArray | null = section.match(R.level);

if (levelMatch && levelMatch.groups) {
const levelId = levelMatch.groups.levelId.replace(".", "");
Expand All@@ -77,8 +83,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
};
} else {
// match step
const stepRegex = /^(#{3}\s(?<stepTitle>.*)[\n\r]+(?<stepContent>[^]*))/;
const stepMatch: RegExpMatchArray | null = section.match(stepRegex);
const stepMatch: RegExpMatchArray | null = section.match(R.step);
if (stepMatch && stepMatch.groups) {
current = {
levelId: current.levelId,
Expand All@@ -91,17 +96,14 @@ export function parseMdContent(md: string): TutorialFrame | never {
content: stepContent.trim(),
};
} else {
const hintDetectRegex = /^(#{4}\sHINTS[\n\r]+([\*|\-]\s(?<hintContent>[^]*))[\n\r]+)+/;
const hintMatch = section.match(hintDetectRegex);
const subtaskDetectRegex = /^(#{4}\sSUBTASKS[\n\r]+([\*|\-]\s(?<subtaskContent>[^]*))[\n\r]+)+/;
const subtaskMatch = section.match(subtaskDetectRegex);
const listItemregex = /[\n\r]+[\*|\-]\s/;
const hintMatch = section.match(R.hints);
const subtaskMatch = section.match(R.subtasks);

switch (true) {
// parse hints from stepContent
case !!hintMatch:
const hints = section
.split(listItemregex)
.split(R.listItem)
.slice(1) // remove #### HINTS
.map((h) => h.trim());
if (hints.length) {
Expand All@@ -113,7 +115,7 @@ export function parseMdContent(md: string): TutorialFrame | never {
// parse subtasks from stepContent
case !!subtaskMatch:
const subtasks = section
.split(listItemregex)
.split(R.listItem)
.slice(1) // remove #### SUBTASKS
.map((h) => h.trim());
if (subtasks.length) {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp