WIP md parser
This commit is contained in:
15
build.js
15
build.js
@@ -18,9 +18,13 @@ const paths = {
|
||||
|
||||
if (fs.readdirSync(".").includes(paths.output)) {
|
||||
fs.rmSync(paths.output, { recursive: true, force: true });
|
||||
console.log("Starting rebuild");
|
||||
} else {
|
||||
console.log("Starting build");
|
||||
}
|
||||
fs.mkdirSync(paths.output);
|
||||
|
||||
console.log("Indexing content..");
|
||||
const directoryNames = fs.readdirSync(paths.notes.root);
|
||||
const manifest = directoryNames.map((noteDirectory) => ({
|
||||
directoryName: noteDirectory,
|
||||
@@ -33,6 +37,7 @@ const manifest = directoryNames.map((noteDirectory) => ({
|
||||
publicAssetDirectoryPath: `/${noteDirectory}_`,
|
||||
}));
|
||||
|
||||
console.log("Parsing content..");
|
||||
manifest.forEach((m) => {
|
||||
const notePath = `${paths.templates.root}/${paths.templates.note}`;
|
||||
let htmlTemplate = fs.readFileSync(notePath, {
|
||||
@@ -57,6 +62,7 @@ manifest.forEach((m) => {
|
||||
);
|
||||
});
|
||||
|
||||
console.log("Moving static templates..");
|
||||
[paths.templates.notFound].forEach((filename) => {
|
||||
fs.copyFileSync(
|
||||
`${paths.templates.root}/${filename}`,
|
||||
@@ -64,6 +70,7 @@ manifest.forEach((m) => {
|
||||
);
|
||||
});
|
||||
|
||||
console.log("Building startpage..");
|
||||
let htmlTemplate = fs.readFileSync(
|
||||
`${paths.templates.root}/${paths.templates.index}`,
|
||||
{
|
||||
@@ -71,9 +78,9 @@ let htmlTemplate = fs.readFileSync(
|
||||
}
|
||||
);
|
||||
|
||||
const links = manifest.map(
|
||||
(m) => `<a href='/${m.directoryName}.html'>${m.name}</a>`
|
||||
);
|
||||
const links = manifest
|
||||
.map((m) => `<a href='/${m.directoryName}.html'>${m.name}</a>`)
|
||||
.reverse();
|
||||
|
||||
const unorderedListItems = links.map((l) => `<li>${l}</li>`).join("\r\n");
|
||||
const html = `
|
||||
@@ -86,3 +93,5 @@ fs.writeFileSync(`${paths.output}/index.html`, htmlTemplate, {
|
||||
encoding: "utf-8",
|
||||
flag: "ax",
|
||||
});
|
||||
|
||||
console.log("Done");
|
||||
|
||||
@@ -542,8 +542,7 @@ class SingleLineCode extends Symbol {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
const trimmedLine = line.trim();
|
||||
return trimmedLine.startsWith("`");
|
||||
return line.startsWith("`");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -917,9 +916,7 @@ const stageThreeProcessing = (symbols) => {
|
||||
);
|
||||
const paragraph = new Paragraph();
|
||||
paragraph.children = children;
|
||||
console.log(children, startIndex, endIndex);
|
||||
localSymbols.splice(startIndex, children.length + 1, paragraph);
|
||||
console.log(localSymbols.filter((s) => s instanceof JustALineBreak).length);
|
||||
}
|
||||
// Fix indentation of bullet elements
|
||||
return localSymbols;
|
||||
|
||||
21
notes/4-markdown-parser-part-2/note.md
Normal file
21
notes/4-markdown-parser-part-2/note.md
Normal 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.
|
||||
Reference in New Issue
Block a user