Fixed processing of text rows into children
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -66,3 +66,4 @@ node_modules/
|
||||
.env
|
||||
|
||||
.dist
|
||||
temp
|
||||
117
markdown.js
117
markdown.js
@@ -396,8 +396,7 @@ class Italic extends Symbol {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
const trimmedLine = line.trim();
|
||||
return trimmedLine.startsWith("_");
|
||||
return line.startsWith("_");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -454,8 +453,7 @@ class Bold extends Symbol {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
const trimmedLine = line.trim();
|
||||
return trimmedLine.startsWith("**");
|
||||
return line.startsWith("**");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -636,11 +634,27 @@ class MultiLineCode extends Symbol {
|
||||
}
|
||||
}
|
||||
|
||||
class Text extends Symbol {
|
||||
class TextRow extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
text = "";
|
||||
/**
|
||||
* @type {Symbol[]}
|
||||
*/
|
||||
children = [];
|
||||
/**
|
||||
* @type {Symbol[]}
|
||||
*/
|
||||
allowedChildren = [
|
||||
Bold,
|
||||
Italic,
|
||||
ImageLink,
|
||||
Link,
|
||||
MultiLineCode,
|
||||
SingleLineCode,
|
||||
TextRow,
|
||||
];
|
||||
/**
|
||||
*
|
||||
* @param {string} line
|
||||
@@ -656,7 +670,69 @@ class Text extends Symbol {
|
||||
* @returns {Symbol}
|
||||
*/
|
||||
static create(lineFeed) {
|
||||
const instance = new Text();
|
||||
const instance = new TextRow();
|
||||
instance.text = lineFeed.claim();
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} assetDirectory
|
||||
* @returns {void}
|
||||
*/
|
||||
process(assetDirectory) {
|
||||
for (let i = 0; i < this.text.length; i++) {
|
||||
if (i === this.text.length - 1) {
|
||||
const plainText = new PlainText();
|
||||
plainText.text = this.text;
|
||||
this.children = [plainText];
|
||||
break;
|
||||
}
|
||||
const line = this.text.slice(i);
|
||||
const symbols = toSymbols(line, assetDirectory, this.allowedChildren);
|
||||
const symbolsIsOnlyOneTextRow =
|
||||
symbols.length === 1 && symbols[0] instanceof TextRow;
|
||||
if (!symbols.length || symbolsIsOnlyOneTextRow) {
|
||||
continue;
|
||||
}
|
||||
const plainText = new PlainText();
|
||||
plainText.text = this.text.slice(0, i);
|
||||
this.children = [plainText, ...symbols];
|
||||
break;
|
||||
}
|
||||
this.children.forEach((c) => c.process(assetDirectory));
|
||||
}
|
||||
|
||||
render() {
|
||||
return this.children.map((c) => c.render()).join("");
|
||||
}
|
||||
}
|
||||
|
||||
class PlainText extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
text = "";
|
||||
/**
|
||||
* @type {Symbol[]}
|
||||
*/
|
||||
children = [];
|
||||
/**
|
||||
*
|
||||
* @param {string} line
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {LineFeed} lineFeed
|
||||
* @returns {Symbol}
|
||||
*/
|
||||
static create(lineFeed) {
|
||||
const instance = new PlainText();
|
||||
instance.text = lineFeed.claim();
|
||||
return instance;
|
||||
}
|
||||
@@ -747,7 +823,6 @@ const getAmountOfTokenInBeginningOfFile = (token, string) => {
|
||||
* @type {Symbol[]}
|
||||
*/
|
||||
const AllSymbols = [
|
||||
Paragraph,
|
||||
Heading,
|
||||
UnorderedListItem,
|
||||
OrderedListItem,
|
||||
@@ -757,24 +832,9 @@ const AllSymbols = [
|
||||
Link,
|
||||
MultiLineCode,
|
||||
SingleLineCode,
|
||||
Text,
|
||||
];
|
||||
/**
|
||||
* @type {Symbol[]}
|
||||
*/
|
||||
const AllSymbolsExceptParagraph = [
|
||||
Paragraph,
|
||||
Heading,
|
||||
UnorderedListItem,
|
||||
OrderedListItem,
|
||||
Bold,
|
||||
Italic,
|
||||
ImageLink,
|
||||
Link,
|
||||
MultiLineCode,
|
||||
SingleLineCode,
|
||||
Text,
|
||||
TextRow,
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} markdown
|
||||
@@ -782,11 +842,16 @@ const AllSymbolsExceptParagraph = [
|
||||
* @returns {string}
|
||||
*/
|
||||
export const toHtml = (markdown, assetDirectory) => {
|
||||
// Stage one, markdown to symbols
|
||||
const symbols = toSymbols(markdown, assetDirectory);
|
||||
|
||||
// Stage two, expanding symbols
|
||||
console.log("starting processing");
|
||||
symbols.forEach((s) => s.process(assetDirectory));
|
||||
|
||||
// Stage three, operations based on symbol relationship
|
||||
|
||||
// Stage four, rendering
|
||||
const html = symbols.map((s) => s.render()).join("");
|
||||
return `<div>${html}</div>`;
|
||||
};
|
||||
@@ -798,13 +863,13 @@ export const toHtml = (markdown, assetDirectory) => {
|
||||
* @param {Symbol[]} allowedSymbols
|
||||
* @returns {Symbol[]}
|
||||
*/
|
||||
const toSymbols = (markdown, assetDirectory, allowedSymbols) => {
|
||||
const toSymbols = (markdown, assetDirectory, allowedSymbols = AllSymbols) => {
|
||||
const lineFeed = new LineFeed(markdown);
|
||||
const symbols = [];
|
||||
|
||||
while (!lineFeed.isEmpty()) {
|
||||
const line = lineFeed.peek();
|
||||
const factory = AllSymbols.find((factory) => factory.canParse(line));
|
||||
const factory = allowedSymbols.find((factory) => factory.canParse(line));
|
||||
const symbol = factory.create(lineFeed, assetDirectory);
|
||||
symbols.push(symbol);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
_2023-09-15_
|
||||
|
||||
I've been thinking a long time about having a place to publicly publish things.
|
||||
I'm not really into doing that on social media since it would lock my content in their format and make it hard to move anywhere else so i thought i would just write my "things" in plain markdown and then find a way of hosting them online.
|
||||
I've been thinking a long time about having a place to publicly publish things. I'm not really into doing that on social media since it would lock my content in their format and make it hard to move anywhere else so i thought i would just write my "things" in plain markdown and then find a way of hosting them online.
|
||||
|
||||
Sure there's a lot of static site generators out there and a couple of them could probably be configured to work the way i want it to. However to me there is a joy in using things that I have built on my own so first i will try building something from scratch.
|
||||
|
||||
@@ -37,6 +36,8 @@ _Italics_
|
||||
|
||||
_Italics_ then text
|
||||
|
||||
Text then _italics_
|
||||
|
||||
**Bold**
|
||||
|
||||
**Bold** then text
|
||||
@@ -74,7 +75,3 @@ const y = () => {
|
||||
[link to the index](https://blog.zacke.dev) with text after it
|
||||
|
||||

|
||||
|
||||
rstd
|
||||
rastd
|
||||
rstd
|
||||
|
||||
Reference in New Issue
Block a user