-
Notifications
You must be signed in to change notification settings - Fork 1
starting generate JSON file for module #99
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| import { readFile, readdir, writeFile, mkdir } from 'fs/promises' | ||
| import { parse, join } from 'path' | ||
|
|
||
| import * as esbuild from 'esbuild' | ||
|
|
||
| import { fromMarkdown } from 'mdast-util-from-markdown' | ||
| import { rootDir, DEV, time } from './utils.js' | ||
| import { generateModJSON, parseContent } from './module-parser.js' | ||
|
|
||
| const getHash = async head => { | ||
| if (!head.startsWith('ref:')) return { hash: head.trim(), branch: 'detached' } | ||
|
|
@@ -12,7 +12,7 @@ const getHash = async head => { | |
| const hash = await readFile(join(rootDir, '.git', ...parts), 'utf8') | ||
| return { hash: hash.trim(), branch } | ||
| } | ||
|
|
||
| try { | ||
| const head = await readFile(join(rootDir, '.git/HEAD'), 'utf8') | ||
| const { hash, branch } = await getHash(head) | ||
|
|
@@ -23,6 +23,23 @@ try { | |
| process.env.HASH = `unk@${now.toString(36)}` | ||
| } | ||
|
|
||
| export const modJsDir = () => readdir(join(rootDir, 'js-module')) | ||
|
|
||
| export const bundleJSONDir = (dirName) => | ||
| mkdir(join(rootDir, dirName), { recursive: true }) | ||
|
|
||
| export const readJSMod = async () => { | ||
| const dirList = await modJsDir() | ||
| const entries = await Promise.all( | ||
| dirList.map(async (name) => { | ||
| const file = await readFile(join(rootDir, 'js-module', name)) | ||
| const root = fromMarkdown(file) | ||
| return [name, parseContent(root.children)] | ||
| }), | ||
| ) | ||
| return entries | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pareil, pas sur que ca soit specialement une bonne abstraction, a la limite faire un const readdirContent = path => {
const files = readdir(join(rootDir, path))
const pendingEntries = files.map(async (fileName) => [
fileName,
await readFile(join(rootDir, path, fileName)),
])
return Promise.all(pendingEntries)
}Plus generique et re-utilisable, et tu fait la passe de markdown apres |
||
|
|
||
| const templateDir = join(rootDir, 'template') | ||
| const readEntry = async ({ name, ext, base }) => [ | ||
| name, | ||
|
|
@@ -51,6 +68,7 @@ const config = { | |
|
|
||
| const serve = () => esbuild.serve({ servedir }, config) | ||
| const generate = async (file = 'index') => { | ||
| await generateModJSON() | ||
| const content = await readdir(templateDir) | ||
| const entries = await Promise.all(content.map(parse).map(readEntry)) | ||
| const templates = Object.fromEntries(entries) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { readdir, writeFile, rename } from 'fs/promises' | ||
| import { join } from 'path' | ||
| import { readJSMod, modJsDir, bundleJSONDir } from './build.js' | ||
| import { rootDir } from './utils.js' | ||
|
|
||
| // returns a flatten array of all the children | ||
| const children = (n) => | ||
| n.children ? [n, ...n.children.flatMap(children)] : [n] | ||
|
|
||
| const getTrimValue = (n) => n.value?.trim() | ||
| const textContent = (n) => | ||
| children(n).map(getTrimValue).filter(Boolean).join(' ') || '' | ||
|
|
||
| const isH1 = (node) => node.type === 'heading' && node.depth === 1 | ||
| const isH2 = (node) => node.type === 'heading' && node.depth === 2 | ||
| const isH3 = (node) => node.type === 'heading' && node.depth === 3 | ||
| const isP = (node) => node.type === 'paragraph' | ||
| const isCODE = (node) => node.type === 'code' | ||
| const isLI = (node) => node.type === 'list' | ||
|
|
||
| export const parseContent = (nodeList) => { | ||
| const content = { description: '' } | ||
| let mode, exercise = isP | ||
| for (const node of nodeList) { | ||
| if (!content.title && isH1(node)) { | ||
| content.title = textContent(node) | ||
| } else if (isH2(node)) { | ||
| mode = textContent(node).toLowerCase() | ||
| content[mode] = [] | ||
| } else if (mode === 'notions') { | ||
| if (isLI(node)) { | ||
| content.notions = children(node).map(getTrimValue).filter(Boolean) | ||
| } | ||
| } else if (mode === 'description') { | ||
| if (isP(node)) { | ||
| content.description = children(node) | ||
| .map(getTrimValue) | ||
| .filter(Boolean) | ||
| .join(' ') | ||
| } | ||
| } else if (mode === 'exercise') { | ||
| if (isH3(node)) { | ||
| exercise = { name: textContent(node) } | ||
| content.exercise.push(exercise) | ||
| } else if ( exercise && isCODE(node)) { | ||
| console.log(node) | ||
| exercise.code = textContent(node) | ||
| exercise.lang = node.lang | ||
| } else { | ||
| console.warn('ignored node', node) | ||
| } | ||
| } else if (mode) { | ||
| // any other mode is stored in raw tree | ||
| content[mode].push(node) | ||
| } else { | ||
| // before any mode is set, we are writing the description | ||
| content.description += `${textContent(node).trim()}\n` | ||
| } | ||
| } | ||
| return content | ||
| } | ||
|
|
||
| export const generateModJSON = async () => { | ||
| const dirList = await modJsDir() | ||
| const entries = await readJSMod() | ||
| const modulebundle = await bundleJSONDir('modulebundle') | ||
| dirList.map(async (file) => { | ||
| const data = Object.fromEntries(entries)[file] | ||
| await writeFile(`${file.split('.md')[0]}.json`, JSON.stringify(data)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }) | ||
|
|
||
| await (await readdir(rootDir)) | ||
| .filter((filename) => filename.includes('module.json')) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .map((e) => rename(join(rootDir, e), join(modulebundle, e))) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # JS Basics | ||
|
|
||
| ## Description | ||
|
|
||
| This module talk about the Basics of Javascript Programming | ||
|
|
||
| ## Exercise | ||
|
|
||
| ### Hello There | ||
|
|
||
| ```js | ||
| { name: "Hello There", type: "exercise", key: "01-hello-there.exercise.md"} | ||
| ``` | ||
|
|
||
| ### Anything to declare | ||
|
|
||
| ```js | ||
| { name: "Anything to declare", type: "exercise", key: "02-anything-to-declare.exercise.md"} | ||
| ``` | ||
|
|
||
| ### Undefined future | ||
|
|
||
| ```js | ||
| { name: "Undefined future", type: "exercise", key: "03-undefined-future.exercise.md"} | ||
| ``` | ||
|
|
||
| ### The great escape | ||
|
|
||
| ```js | ||
| { name: "The great escape", type: "exercise", key: "04-the-great-escape.exercise.md"} | ||
| ``` | ||
|
|
||
| ### String of number | ||
|
|
||
| ```js | ||
| { name: "String of number", type: "exercise", key: "05-string-of-number.exercise.md"} | ||
| ``` | ||
|
|
||
| ### Redecleration of love | ||
|
|
||
| ```js | ||
| { name: "Redecleration of love", type: "exercise", key: "06-redecleration-of-love.exercise.md"} | ||
| ``` | ||
|
|
||
| ### Smooth operator | ||
|
|
||
| ```js | ||
| { name: "Smooth operator", type: "exercise", key: "07-smooth-operator.exercise.md"} | ||
| ``` | ||
|
|
||
| ### Placeholders | ||
|
|
||
| ```js | ||
| { name: "Placeholders", type: "exercise", key: "08-placeholders.exercise.md"} | ||
| ``` | ||
|
|
||
| ### Duplicate | ||
|
|
||
| ```js | ||
| { name: "Duplicate", type: "exercise", key: "09-duplicate.exercise.md"} | ||
| ``` | ||
|
|
||
| ## Quizz | ||
|
|
||
| ## Notions | ||
|
|
||
| - variables | ||
| - data types | ||
| - console | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya pas de markdown vraiment a parser pour les modules, tu peu gerer un README mais la il aura pas ce genre de contenu, juste une description libre. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pas convaincu de l'utilite de cette fonction, elle abstrait les mauvaise chose a mon avis.
A la limite fait un
readdirFromRoot = path => readdir(join(rootDir, path))Mais franchement on gagne pas grand chose vs juste faire le call