Fixed processing of text rows into children
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user