Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 5, 2020
Merged
Changes from 1 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
Prev Previous commit
refactor parse regexes
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jul 5, 2020
commit 859d8e526706cce0666dae9a42012113c8747d9d
30 changes: 16 additions & 14 deletions src/utils/parse.ts
Original file line number Diff line number Diff 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 Down Expand 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