Fixed paragraphs in stage three processing
This commit is contained in:
83
markdown.js
83
markdown.js
@@ -116,6 +116,41 @@ class Heading extends Symbol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class JustALineBreak extends Symbol {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} line
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
static canParse(line) {
|
||||||
|
return line === "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {LineFeed} lineFeed
|
||||||
|
* @returns {Symbol}
|
||||||
|
*/
|
||||||
|
static create(lineFeed) {
|
||||||
|
const instance = new JustALineBreak();
|
||||||
|
lineFeed.claim();
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} assetDirectory
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
process(assetDirectory) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
throw new Error("Symbol cannot be rendered directly");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Paragraph extends Symbol {
|
class Paragraph extends Symbol {
|
||||||
/**
|
/**
|
||||||
* @type {string[]}
|
* @type {string[]}
|
||||||
@@ -164,10 +199,7 @@ class Paragraph extends Symbol {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
process(assetDirectory) {
|
process(assetDirectory) {
|
||||||
if (this.lines.length === 0) return;
|
return;
|
||||||
const symbols = toSymbols(this.lines.join("\n"), assetDirectory);
|
|
||||||
this.lines = [];
|
|
||||||
this.children = symbols;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@@ -823,6 +855,7 @@ const getAmountOfTokenInBeginningOfFile = (token, string) => {
|
|||||||
* @type {Symbol[]}
|
* @type {Symbol[]}
|
||||||
*/
|
*/
|
||||||
const AllSymbols = [
|
const AllSymbols = [
|
||||||
|
JustALineBreak,
|
||||||
Heading,
|
Heading,
|
||||||
UnorderedListItem,
|
UnorderedListItem,
|
||||||
OrderedListItem,
|
OrderedListItem,
|
||||||
@@ -846,16 +879,52 @@ export const toHtml = (markdown, assetDirectory) => {
|
|||||||
const symbols = toSymbols(markdown, assetDirectory);
|
const symbols = toSymbols(markdown, assetDirectory);
|
||||||
|
|
||||||
// Stage two, expanding symbols
|
// Stage two, expanding symbols
|
||||||
console.log("starting processing");
|
|
||||||
symbols.forEach((s) => s.process(assetDirectory));
|
symbols.forEach((s) => s.process(assetDirectory));
|
||||||
|
|
||||||
// Stage three, operations based on symbol relationship
|
// Stage three, operations based on symbol relationship
|
||||||
|
const stageThree = stageThreeProcessing(symbols);
|
||||||
// Stage four, rendering
|
// Stage four, rendering
|
||||||
const html = symbols.map((s) => s.render()).join("");
|
const html = stageThree
|
||||||
|
// .filter((s) => !(s instanceof JustALineBreak))
|
||||||
|
.map((s) => s.render())
|
||||||
|
.join("");
|
||||||
return `<div>${html}</div>`;
|
return `<div>${html}</div>`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Symbol[]} symbols
|
||||||
|
* @returns {Symbol[]}
|
||||||
|
*/
|
||||||
|
const stageThreeProcessing = (symbols) => {
|
||||||
|
const localSymbols = [...symbols];
|
||||||
|
// Turn line break pairs into paragraphs
|
||||||
|
while (localSymbols.filter((s) => s instanceof JustALineBreak).length !== 0) {
|
||||||
|
const startIndex = localSymbols.findIndex(
|
||||||
|
(s) => s instanceof JustALineBreak
|
||||||
|
);
|
||||||
|
const subEndIndex = localSymbols
|
||||||
|
.slice(startIndex + 1)
|
||||||
|
.findIndex((s) => s instanceof JustALineBreak);
|
||||||
|
const endIndex =
|
||||||
|
subEndIndex === -1
|
||||||
|
? localSymbols.length - 1
|
||||||
|
: startIndex + 1 + subEndIndex;
|
||||||
|
const lastItemIsNotLineBreak = subEndIndex === -1;
|
||||||
|
const children = localSymbols.slice(
|
||||||
|
startIndex + 1,
|
||||||
|
lastItemIsNotLineBreak ? localSymbols.length : endIndex
|
||||||
|
);
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {string} markdown
|
* @param {string} markdown
|
||||||
|
|||||||
Reference in New Issue
Block a user