Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 21 additions & 3 deletions dev/build.js
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' }
Expand All @@ -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)
Expand All @@ -23,6 +23,23 @@ try {
process.env.HASH = `unk@${now.toString(36)}`
}

export const modJsDir = () => readdir(join(rootDir, 'js-module'))
Copy link
Member

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


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
}
Copy link
Member

Choose a reason for hiding this comment

The 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 readdirContent:

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,
Expand Down Expand Up @@ -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)
Expand Down
75 changes: 75 additions & 0 deletions dev/module-parser.js
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file.split('.md')[0]}.json toujours pas une tres bonne facon d'extraire l'extension

})

await (await readdir(rootDir))
.filter((filename) => filename.includes('module.json'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.endsWith plutot que .includes la

.map((e) => rename(join(rootDir, e), join(modulebundle, e)))
}
69 changes: 69 additions & 0 deletions js-module/Readme.module.md
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
Copy link
Member

Choose a reason for hiding this comment

The 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.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"esbuild": "^0.11.2",
"fast-toml": "^0.5.4",
"mdast-util-from-markdown": "^1.0.4",
"preact": "^10.5.13"
}
}