WIP md parser

This commit is contained in:
2023-09-20 11:59:02 +02:00
parent d926e06fa8
commit 1454b301cd
3 changed files with 34 additions and 7 deletions

View File

@@ -18,9 +18,13 @@ const paths = {
if (fs.readdirSync(".").includes(paths.output)) { if (fs.readdirSync(".").includes(paths.output)) {
fs.rmSync(paths.output, { recursive: true, force: true }); fs.rmSync(paths.output, { recursive: true, force: true });
console.log("Starting rebuild");
} else {
console.log("Starting build");
} }
fs.mkdirSync(paths.output); fs.mkdirSync(paths.output);
console.log("Indexing content..");
const directoryNames = fs.readdirSync(paths.notes.root); const directoryNames = fs.readdirSync(paths.notes.root);
const manifest = directoryNames.map((noteDirectory) => ({ const manifest = directoryNames.map((noteDirectory) => ({
directoryName: noteDirectory, directoryName: noteDirectory,
@@ -33,6 +37,7 @@ const manifest = directoryNames.map((noteDirectory) => ({
publicAssetDirectoryPath: `/${noteDirectory}_`, publicAssetDirectoryPath: `/${noteDirectory}_`,
})); }));
console.log("Parsing content..");
manifest.forEach((m) => { manifest.forEach((m) => {
const notePath = `${paths.templates.root}/${paths.templates.note}`; const notePath = `${paths.templates.root}/${paths.templates.note}`;
let htmlTemplate = fs.readFileSync(notePath, { let htmlTemplate = fs.readFileSync(notePath, {
@@ -57,6 +62,7 @@ manifest.forEach((m) => {
); );
}); });
console.log("Moving static templates..");
[paths.templates.notFound].forEach((filename) => { [paths.templates.notFound].forEach((filename) => {
fs.copyFileSync( fs.copyFileSync(
`${paths.templates.root}/${filename}`, `${paths.templates.root}/${filename}`,
@@ -64,6 +70,7 @@ manifest.forEach((m) => {
); );
}); });
console.log("Building startpage..");
let htmlTemplate = fs.readFileSync( let htmlTemplate = fs.readFileSync(
`${paths.templates.root}/${paths.templates.index}`, `${paths.templates.root}/${paths.templates.index}`,
{ {
@@ -71,9 +78,9 @@ let htmlTemplate = fs.readFileSync(
} }
); );
const links = manifest.map( const links = manifest
(m) => `<a href='/${m.directoryName}.html'>${m.name}</a>` .map((m) => `<a href='/${m.directoryName}.html'>${m.name}</a>`)
); .reverse();
const unorderedListItems = links.map((l) => `<li>${l}</li>`).join("\r\n"); const unorderedListItems = links.map((l) => `<li>${l}</li>`).join("\r\n");
const html = ` const html = `
@@ -86,3 +93,5 @@ fs.writeFileSync(`${paths.output}/index.html`, htmlTemplate, {
encoding: "utf-8", encoding: "utf-8",
flag: "ax", flag: "ax",
}); });
console.log("Done");

View File

@@ -542,8 +542,7 @@ class SingleLineCode extends Symbol {
* @returns {boolean} * @returns {boolean}
*/ */
static canParse(line) { static canParse(line) {
const trimmedLine = line.trim(); return line.startsWith("`");
return trimmedLine.startsWith("`");
} }
/** /**
@@ -917,9 +916,7 @@ const stageThreeProcessing = (symbols) => {
); );
const paragraph = new Paragraph(); const paragraph = new Paragraph();
paragraph.children = children; paragraph.children = children;
console.log(children, startIndex, endIndex);
localSymbols.splice(startIndex, children.length + 1, paragraph); localSymbols.splice(startIndex, children.length + 1, paragraph);
console.log(localSymbols.filter((s) => s instanceof JustALineBreak).length);
} }
// Fix indentation of bullet elements // Fix indentation of bullet elements
return localSymbols; return localSymbols;

View File

@@ -0,0 +1,21 @@
# Markdown parser part 2
The markdown parser is kind of going according to plan. I realised that with the appoach I decided to take I'm going to need a couple of more steps than just going line by line and deciding what type of element each should be.
Let's take an example:
```
Some **bold** text and then some _cursive_ text
```
This is not only a text line but it has both bold and cursive text.
So in addition to parsing the markdown files line by line (stage 1), I implemented stage 2 processing with the purpose of expanding the identified elements into child elements. So when a text row element containing the text in the example above is requested to peform its stage 2 processing it will take its text and run it once more through the stage 1 processing to divide it into new symbols. It then calls stage 2 processing of all its new children to make sure every element has been processed.
In the end we should have gone from `TextLine` to `PlainText, Bold, PlainText , Italic, Plaintext`.
Here's the result.
Some **bold** text and then some _cursive_ text
Next up is stage 3 processing which will affect elements depending on their position in the list of elements.