Nested processing for paragraphs

This commit is contained in:
2023-09-19 19:03:15 +02:00
parent 04c1e12ed5
commit 0c48dc81be

View File

@@ -164,19 +164,14 @@ class Paragraph extends Symbol {
* @returns {void} * @returns {void}
*/ */
process(assetDirectory) { process(assetDirectory) {
return;
if (this.lines.length === 0) return; if (this.lines.length === 0) return;
const symbols = toSymbols(this.lines.join("\n"), assetDirectory); const symbols = toSymbols(this.lines.join("\n"), assetDirectory);
const lastIterationSymbolsAsJson = JSON.stringify(symbols);
// do {
// symbols.forEach((s) => s.process());
// } while (JSON.stringify(symbols) !== lastIterationSymbolsAsJson);
this.lines = []; this.lines = [];
this.children = symbols; this.children = symbols;
} }
render() { render() {
return `<p>${this.lines.join("")}</p>`; return `<p>${this.children.map((c) => c.render()).join("")}</p>`;
} }
} }
@@ -751,7 +746,23 @@ const getAmountOfTokenInBeginningOfFile = (token, string) => {
/** /**
* @type {Symbol[]} * @type {Symbol[]}
*/ */
const Factories = [ const AllSymbols = [
Paragraph,
Heading,
UnorderedListItem,
OrderedListItem,
Bold,
Italic,
ImageLink,
Link,
MultiLineCode,
SingleLineCode,
Text,
];
/**
* @type {Symbol[]}
*/
const AllSymbolsExceptParagraph = [
Paragraph, Paragraph,
Heading, Heading,
UnorderedListItem, UnorderedListItem,
@@ -764,7 +775,6 @@ const Factories = [
SingleLineCode, SingleLineCode,
Text, Text,
]; ];
/** /**
* *
* @param {string} markdown * @param {string} markdown
@@ -774,11 +784,8 @@ const Factories = [
export const toHtml = (markdown, assetDirectory) => { export const toHtml = (markdown, assetDirectory) => {
const symbols = toSymbols(markdown, assetDirectory); const symbols = toSymbols(markdown, assetDirectory);
let lastIterationsSymbolsAsJson = JSON.stringify(symbols);
do {
console.log("starting processing"); console.log("starting processing");
symbols.forEach((s) => s.process(assetDirectory)); symbols.forEach((s) => s.process(assetDirectory));
} while (JSON.stringify(symbols) !== lastIterationsSymbolsAsJson);
const html = symbols.map((s) => s.render()).join(""); const html = symbols.map((s) => s.render()).join("");
return `<div>${html}</div>`; return `<div>${html}</div>`;
@@ -788,15 +795,16 @@ export const toHtml = (markdown, assetDirectory) => {
* *
* @param {string} markdown * @param {string} markdown
* @param {string} assetDirectory * @param {string} assetDirectory
* @param {Symbol[]} allowedSymbols
* @returns {Symbol[]} * @returns {Symbol[]}
*/ */
const toSymbols = (markdown, assetDirectory) => { const toSymbols = (markdown, assetDirectory, allowedSymbols) => {
const lineFeed = new LineFeed(markdown); const lineFeed = new LineFeed(markdown);
const symbols = []; const symbols = [];
while (!lineFeed.isEmpty()) { while (!lineFeed.isEmpty()) {
const line = lineFeed.peek(); const line = lineFeed.peek();
const factory = Factories.find((factory) => factory.canParse(line)); const factory = AllSymbols.find((factory) => factory.canParse(line));
const symbol = factory.create(lineFeed, assetDirectory); const symbol = factory.create(lineFeed, assetDirectory);
symbols.push(symbol); symbols.push(symbol);
} }