mdquiz 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +139 -0
- package/dist/index.d.mts +59 -0
- package/dist/index.mjs +138 -0
- package/package.json +70 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-PRESENT Aleksander Fidelus <https://github.com/FidelusAleksander>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# mdquiz
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![bundle][bundle-src]][bundle-href]
|
|
6
|
+
[![License][license-src]][license-href]
|
|
7
|
+
|
|
8
|
+
Parse markdown quiz files into structured objects. Supports YAML frontmatter, checkbox answers, code blocks, and hint blockquotes.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install mdquiz
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Parse a single file
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { readFileSync } from 'node:fs'
|
|
22
|
+
import { parseQuestionFile } from 'mdquiz'
|
|
23
|
+
|
|
24
|
+
const md = readFileSync('question-001.md', 'utf-8')
|
|
25
|
+
const question = parseQuestionFile(md, 'question-001')
|
|
26
|
+
|
|
27
|
+
console.log(question.question) // "Which syntax defines a job?"
|
|
28
|
+
console.log(question.answers) // [{ id: 'a1', text: '...', isCorrect: true }, ...]
|
|
29
|
+
console.log(question.isMultiSelect) // false
|
|
30
|
+
console.log(question.hint) // "https://docs.github.com/..."
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Parse a directory
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { parseDirectory } from 'mdquiz'
|
|
37
|
+
|
|
38
|
+
const questions = parseDirectory('./questions', {
|
|
39
|
+
filePrefix: 'question-', // only parse files starting with this prefix
|
|
40
|
+
recursive: false, // walk subdirectories (default: false)
|
|
41
|
+
})
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Markdown Format
|
|
45
|
+
|
|
46
|
+
```markdown
|
|
47
|
+
---
|
|
48
|
+
question: "Which GitHub Actions syntax correctly defines a job that runs on Ubuntu?"
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
> https://docs.github.com/en/actions/using-workflows
|
|
52
|
+
|
|
53
|
+
- [x] `runs-on: ubuntu-latest`
|
|
54
|
+
- [ ] `os: ubuntu-latest`
|
|
55
|
+
- [ ] `platform: ubuntu-latest`
|
|
56
|
+
- [ ] `environment: ubuntu-latest`
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Only the `question` field is required in frontmatter. Any additional fields you include (e.g. `title`, `category`, `difficulty`) are accessible via `question.frontmatter`:
|
|
60
|
+
|
|
61
|
+
```markdown
|
|
62
|
+
---
|
|
63
|
+
question: "What is a pull request?"
|
|
64
|
+
difficulty: easy
|
|
65
|
+
tags: ["git", "collaboration"]
|
|
66
|
+
---
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Structure
|
|
70
|
+
|
|
71
|
+
| Section | Required | Description |
|
|
72
|
+
|---------|----------|-------------|
|
|
73
|
+
| Frontmatter | Yes | YAML with `question` field (and any custom fields) |
|
|
74
|
+
| Blockquote | No | Hint text or URL (lines starting with `>`) |
|
|
75
|
+
| Code block | No | Context code shown before answers |
|
|
76
|
+
| Answers | Yes | Checkbox list: `- [x]` correct, `- [ ]` incorrect |
|
|
77
|
+
|
|
78
|
+
### Answer formats
|
|
79
|
+
|
|
80
|
+
Both ordered and unordered lists work:
|
|
81
|
+
|
|
82
|
+
```markdown
|
|
83
|
+
- [x] Correct answer
|
|
84
|
+
- [ ] Wrong answer
|
|
85
|
+
|
|
86
|
+
1. [x] Correct answer
|
|
87
|
+
1. [ ] Wrong answer
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Multiple `[x]` marks make the question multi-select automatically.
|
|
91
|
+
|
|
92
|
+
## API
|
|
93
|
+
|
|
94
|
+
### `parseQuestionFile(content: string, id: string): Question`
|
|
95
|
+
|
|
96
|
+
Parse a markdown string into a Question object.
|
|
97
|
+
|
|
98
|
+
### `parseDirectory(dir: string, options?: ParseDirOptions): Question[]`
|
|
99
|
+
|
|
100
|
+
Parse all `.md` files in a directory. Skips files starting with `_`.
|
|
101
|
+
|
|
102
|
+
### Types
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
interface Question {
|
|
106
|
+
id: string
|
|
107
|
+
question: string
|
|
108
|
+
answers: AnswerOption[]
|
|
109
|
+
isMultiSelect: boolean
|
|
110
|
+
hint?: string
|
|
111
|
+
codeBlock?: string
|
|
112
|
+
frontmatter: Record<string, unknown>
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface AnswerOption {
|
|
116
|
+
id: string
|
|
117
|
+
text: string
|
|
118
|
+
isCorrect: boolean
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
interface ParseDirOptions {
|
|
122
|
+
recursive?: boolean
|
|
123
|
+
filePrefix?: string
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
[MIT](./LICENSE.md) License
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
[npm-version-src]: https://img.shields.io/npm/v/mdquiz?style=flat&colorA=080f12&colorB=1fa669
|
|
133
|
+
[npm-version-href]: https://npmjs.com/package/mdquiz
|
|
134
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/mdquiz?style=flat&colorA=080f12&colorB=1fa669
|
|
135
|
+
[npm-downloads-href]: https://npmjs.com/package/mdquiz
|
|
136
|
+
[bundle-src]: https://img.shields.io/bundlephobia/minzip/mdquiz?style=flat&colorA=080f12&colorB=1fa669&label=minzip
|
|
137
|
+
[bundle-href]: https://bundlephobia.com/result?p=mdquiz
|
|
138
|
+
[license-src]: https://img.shields.io/github/license/FidelusAleksander/mdquiz.svg?style=flat&colorA=080f12&colorB=1fa669
|
|
139
|
+
[license-href]: https://github.com/FidelusAleksander/mdquiz/blob/main/LICENSE.md
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/** A single answer option */
|
|
3
|
+
interface AnswerOption {
|
|
4
|
+
id: string;
|
|
5
|
+
text: string;
|
|
6
|
+
isCorrect: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** A parsed quiz question */
|
|
9
|
+
interface Question {
|
|
10
|
+
/** Unique identifier derived from filename */
|
|
11
|
+
id: string;
|
|
12
|
+
/** The question text (from frontmatter) */
|
|
13
|
+
question: string;
|
|
14
|
+
/** Available answers */
|
|
15
|
+
answers: AnswerOption[];
|
|
16
|
+
/** Whether multiple answers are correct */
|
|
17
|
+
isMultiSelect: boolean;
|
|
18
|
+
/** Optional hint URL or text (from blockquote before answers) */
|
|
19
|
+
hint?: string;
|
|
20
|
+
/** Optional code block shown before answers (question context) */
|
|
21
|
+
codeBlock?: string;
|
|
22
|
+
/** All frontmatter fields (title, question, and any custom fields) */
|
|
23
|
+
frontmatter: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/loader.d.ts
|
|
27
|
+
interface ParseDirOptions {
|
|
28
|
+
/** Walk subdirectories recursively. Default: false */
|
|
29
|
+
recursive?: boolean;
|
|
30
|
+
/** Glob-like prefix filter for filenames. Default: none (all .md files) */
|
|
31
|
+
filePrefix?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parse all markdown question files in a directory.
|
|
35
|
+
*
|
|
36
|
+
* Each .md file is parsed via `parseQuestionFile`. The filename (without
|
|
37
|
+
* extension) becomes the question ID.
|
|
38
|
+
*
|
|
39
|
+
* @param dir - Path to directory containing .md files
|
|
40
|
+
* @param options - Optional config for recursion and filtering
|
|
41
|
+
* @returns Array of parsed questions
|
|
42
|
+
*/
|
|
43
|
+
declare function parseDirectory(dir: string, options?: ParseDirOptions): Question[];
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/parser.d.ts
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Parse a markdown string into a Question object.
|
|
49
|
+
*
|
|
50
|
+
* Expected format: YAML frontmatter with a `question` field,
|
|
51
|
+
* followed by optional hints (blockquotes), optional code blocks,
|
|
52
|
+
* and checkbox-list answers (`- [x]` or `- [ ]`).
|
|
53
|
+
*
|
|
54
|
+
* @param content - Raw markdown string
|
|
55
|
+
* @param id - Unique identifier for this question (e.g. derived from filename)
|
|
56
|
+
*/
|
|
57
|
+
declare function parseQuestionFile(content: string, id: string): Question;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { type AnswerOption, type ParseDirOptions, type Question, parseDirectory, parseQuestionFile };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import matter from "gray-matter";
|
|
4
|
+
|
|
5
|
+
//#region src/parser.ts
|
|
6
|
+
/** Matches an answer line: `- [x] text` or `1. [x] text` */
|
|
7
|
+
const ANSWER_RE = /^(?:\d+\.|-) \[([x ])\] (.*)$/i;
|
|
8
|
+
/** Matches a blockquote line */
|
|
9
|
+
const BLOCKQUOTE_RE = /^> ?(.*)$/;
|
|
10
|
+
/** Matches a code fence open/close */
|
|
11
|
+
const FENCE_RE = /^```/;
|
|
12
|
+
/**
|
|
13
|
+
* Parse a markdown string into a Question object.
|
|
14
|
+
*
|
|
15
|
+
* Expected format: YAML frontmatter with a `question` field,
|
|
16
|
+
* followed by optional hints (blockquotes), optional code blocks,
|
|
17
|
+
* and checkbox-list answers (`- [x]` or `- [ ]`).
|
|
18
|
+
*
|
|
19
|
+
* @param content - Raw markdown string
|
|
20
|
+
* @param id - Unique identifier for this question (e.g. derived from filename)
|
|
21
|
+
*/
|
|
22
|
+
function parseQuestionFile(content, id) {
|
|
23
|
+
const { data, content: body } = matter(content);
|
|
24
|
+
const questionText = data.question;
|
|
25
|
+
if (!questionText) throw new Error(`Missing 'question' in frontmatter for ${id}`);
|
|
26
|
+
const lines = body.split("\n");
|
|
27
|
+
const answerStarts = [];
|
|
28
|
+
let inFence = false;
|
|
29
|
+
for (let i = 0; i < lines.length; i++) {
|
|
30
|
+
if (FENCE_RE.test(lines[i])) {
|
|
31
|
+
inFence = !inFence;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (!inFence && ANSWER_RE.test(lines[i])) answerStarts.push(i);
|
|
35
|
+
}
|
|
36
|
+
if (answerStarts.length === 0) throw new Error(`No answers found in ${id}`);
|
|
37
|
+
const preambleLines = lines.slice(0, answerStarts[0]);
|
|
38
|
+
let hint;
|
|
39
|
+
const codeBlockLines = [];
|
|
40
|
+
let inPreambleFence = false;
|
|
41
|
+
for (const line of preambleLines) {
|
|
42
|
+
if (FENCE_RE.test(line)) {
|
|
43
|
+
inPreambleFence = !inPreambleFence;
|
|
44
|
+
codeBlockLines.push(line);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (inPreambleFence) {
|
|
48
|
+
codeBlockLines.push(line);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const bqMatch = line.match(BLOCKQUOTE_RE);
|
|
52
|
+
if (bqMatch && bqMatch[1].trim()) hint = bqMatch[1].trim();
|
|
53
|
+
}
|
|
54
|
+
const codeBlock = codeBlockLines.length > 0 ? codeBlockLines.join("\n") : void 0;
|
|
55
|
+
const answers = [];
|
|
56
|
+
for (let a = 0; a < answerStarts.length; a++) {
|
|
57
|
+
const startIdx = answerStarts[a];
|
|
58
|
+
const endIdx = a + 1 < answerStarts.length ? answerStarts[a + 1] : lines.length;
|
|
59
|
+
const match = lines[startIdx].match(ANSWER_RE);
|
|
60
|
+
const isCorrect = match[1].toLowerCase() === "x";
|
|
61
|
+
let text = match[2].trim();
|
|
62
|
+
const continuation = [];
|
|
63
|
+
let contFence = false;
|
|
64
|
+
for (let j = startIdx + 1; j < endIdx; j++) {
|
|
65
|
+
const l = lines[j];
|
|
66
|
+
if (FENCE_RE.test(l)) {
|
|
67
|
+
contFence = !contFence;
|
|
68
|
+
continuation.push(l);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (contFence) {
|
|
72
|
+
continuation.push(l);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (BLOCKQUOTE_RE.test(l)) continue;
|
|
76
|
+
if (l.trim() === "") continue;
|
|
77
|
+
continuation.push(l);
|
|
78
|
+
}
|
|
79
|
+
if (continuation.length > 0) text += `\n${continuation.join("\n")}`;
|
|
80
|
+
answers.push({
|
|
81
|
+
isCorrect,
|
|
82
|
+
text
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const correctCount = answers.filter((a) => a.isCorrect).length;
|
|
86
|
+
return {
|
|
87
|
+
id,
|
|
88
|
+
question: questionText,
|
|
89
|
+
answers: answers.map((a, i) => ({
|
|
90
|
+
id: `a${i + 1}`,
|
|
91
|
+
text: a.text,
|
|
92
|
+
isCorrect: a.isCorrect
|
|
93
|
+
})),
|
|
94
|
+
isMultiSelect: correctCount > 1,
|
|
95
|
+
hint,
|
|
96
|
+
codeBlock,
|
|
97
|
+
frontmatter: data
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/loader.ts
|
|
103
|
+
/**
|
|
104
|
+
* Parse all markdown question files in a directory.
|
|
105
|
+
*
|
|
106
|
+
* Each .md file is parsed via `parseQuestionFile`. The filename (without
|
|
107
|
+
* extension) becomes the question ID.
|
|
108
|
+
*
|
|
109
|
+
* @param dir - Path to directory containing .md files
|
|
110
|
+
* @param options - Optional config for recursion and filtering
|
|
111
|
+
* @returns Array of parsed questions
|
|
112
|
+
*/
|
|
113
|
+
function parseDirectory(dir, options = {}) {
|
|
114
|
+
const { recursive = false, filePrefix } = options;
|
|
115
|
+
const questions = [];
|
|
116
|
+
const entries = readdirSync(dir);
|
|
117
|
+
for (const entry of entries.sort()) {
|
|
118
|
+
const fullPath = join(dir, entry);
|
|
119
|
+
if (statSync(fullPath).isDirectory() && recursive) {
|
|
120
|
+
questions.push(...parseDirectory(fullPath, options));
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (!entry.endsWith(".md")) continue;
|
|
124
|
+
if (entry.startsWith("_")) continue;
|
|
125
|
+
if (filePrefix && !entry.startsWith(filePrefix)) continue;
|
|
126
|
+
const id = basename(entry, ".md");
|
|
127
|
+
const content = readFileSync(fullPath, "utf-8");
|
|
128
|
+
try {
|
|
129
|
+
questions.push(parseQuestionFile(content, id));
|
|
130
|
+
} catch (err) {
|
|
131
|
+
console.warn(`Skipping ${fullPath}: ${err.message}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return questions;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
export { parseDirectory, parseQuestionFile };
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mdquiz",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"packageManager": "pnpm@10.33.0",
|
|
6
|
+
"description": "Parse markdown quiz files into structured objects. Supports YAML frontmatter, checkbox answers, code blocks, and hint blockquotes.",
|
|
7
|
+
"author": "Aleksander Fidelus",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://github.com/FidelusAleksander/mdquiz#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/FidelusAleksander/mdquiz.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/FidelusAleksander/mdquiz/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"markdown",
|
|
17
|
+
"quiz",
|
|
18
|
+
"parser",
|
|
19
|
+
"questions",
|
|
20
|
+
"frontmatter",
|
|
21
|
+
"exam"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./dist/index.mjs",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.mjs",
|
|
29
|
+
"module": "./dist/index.mjs",
|
|
30
|
+
"types": "./dist/index.d.mts",
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsdown",
|
|
36
|
+
"dev": "tsdown --watch",
|
|
37
|
+
"lint": "eslint",
|
|
38
|
+
"prepublishOnly": "nr build",
|
|
39
|
+
"release": "bumpp",
|
|
40
|
+
"start": "tsx src/index.ts",
|
|
41
|
+
"test": "vitest",
|
|
42
|
+
"typecheck": "tsc",
|
|
43
|
+
"prepare": "simple-git-hooks"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"gray-matter": "catalog:deps"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@antfu/eslint-config": "catalog:cli",
|
|
50
|
+
"@antfu/ni": "catalog:cli",
|
|
51
|
+
"@types/node": "catalog:types",
|
|
52
|
+
"bumpp": "catalog:cli",
|
|
53
|
+
"eslint": "catalog:cli",
|
|
54
|
+
"lint-staged": "catalog:cli",
|
|
55
|
+
"publint": "catalog:cli",
|
|
56
|
+
"simple-git-hooks": "catalog:cli",
|
|
57
|
+
"tsdown": "catalog:cli",
|
|
58
|
+
"tsnapi": "catalog:testing",
|
|
59
|
+
"tsx": "catalog:cli",
|
|
60
|
+
"typescript": "catalog:cli",
|
|
61
|
+
"vite": "catalog:cli",
|
|
62
|
+
"vitest": "catalog:testing"
|
|
63
|
+
},
|
|
64
|
+
"simple-git-hooks": {
|
|
65
|
+
"pre-commit": "pnpm i --frozen-lockfile --ignore-scripts --offline && pnpm run build && npx lint-staged"
|
|
66
|
+
},
|
|
67
|
+
"lint-staged": {
|
|
68
|
+
"*": "eslint --fix"
|
|
69
|
+
}
|
|
70
|
+
}
|