Rebuild to handle symbol not using whole line

This commit is contained in:
2023-09-16 22:18:27 +02:00
parent b1db4d1316
commit a27c89cdac
3 changed files with 135 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
import * as fs from "fs";
import { toHtml } from "./markdown.js"
import { toHtml } from "./markdown.js";
const paths = {
output: ".dist",
@@ -73,5 +73,3 @@ fs.writeFileSync(`${paths.output}/index.html`, htmlTemplate, {
encoding: "utf-8",
flag: "ax",
});
console.log(manifest);

View File

@@ -31,6 +31,15 @@ class LineFeed {
return line;
}
/**
*
* @param {string} line
*/
push(line) {
if (line === undefined || line === null) return;
this.feed = [line, ...this.feed];
}
/**
*
* @returns {boolean}
@@ -102,7 +111,7 @@ class Italic extends Symbol {
*/
static canParse(line) {
const trimmedLine = line.trim();
return trimmedLine.startsWith("_") && trimmedLine.endsWith("_");
return trimmedLine.startsWith("_");
}
/**
@@ -113,7 +122,23 @@ class Italic extends Symbol {
static create(lineFeed) {
const instance = new Italic();
const line = lineFeed.claim().trim();
const lineWithoutUnderscores = line.slice(1, line.length - 1);
const lineWithoutStartingUnderscore = line.slice(1, line.length);
const lineEndsWithUnderscore = lineWithoutStartingUnderscore.endsWith("_");
if (lineEndsWithUnderscore) {
const lineWithoutUnderscores = lineWithoutStartingUnderscore.slice(
0,
lineWithoutStartingUnderscore.length - 1
);
instance.text = lineWithoutUnderscores;
return instance;
}
const endOfItalicString = lineWithoutStartingUnderscore.indexOf("_");
const lineWithoutUnderscores = lineWithoutStartingUnderscore.slice(
0,
endOfItalicString
);
const rest = lineWithoutStartingUnderscore.slice(endOfItalicString + 1);
lineFeed.push(rest);
instance.text = lineWithoutUnderscores;
return instance;
}
@@ -134,7 +159,7 @@ class Bold extends Symbol {
*/
static canParse(line) {
const trimmedLine = line.trim();
return trimmedLine.startsWith("**") && trimmedLine.endsWith("**");
return trimmedLine.startsWith("**");
}
/**
@@ -145,7 +170,23 @@ class Bold extends Symbol {
static create(lineFeed) {
const instance = new Bold();
const line = lineFeed.claim().trim();
const lineWithoutUnderscores = line.slice(2, line.length - 2);
const lineWithoutStartingUnderscore = line.slice(2, line.length);
const lineEndsWithStars = lineWithoutStartingUnderscore.endsWith("**");
if (lineEndsWithStars) {
const lineWithoutUnderscores = lineWithoutStartingUnderscore.slice(
0,
lineWithoutStartingUnderscore.length - 2
);
instance.text = lineWithoutUnderscores;
return instance;
}
const endOfBoldString = lineWithoutStartingUnderscore.indexOf("**");
const lineWithoutUnderscores = lineWithoutStartingUnderscore.slice(
0,
endOfBoldString
);
const rest = lineWithoutStartingUnderscore.slice(endOfBoldString + 2);
lineFeed.push(rest);
instance.text = lineWithoutUnderscores;
return instance;
}
@@ -154,6 +195,87 @@ class Bold extends Symbol {
}
}
class SingleLineCode extends Symbol {
/**
* @type {string}
*/
text = "";
/**
*
* @param {string} line
* @returns {boolean}
*/
static canParse(line) {
const trimmedLine = line.trim();
return trimmedLine.startsWith("`");
}
/**
*
* @param {LineFeed} lineFeed
* @returns {Symbol}
*/
static create(lineFeed) {
const instance = new SingleLineCode();
const line = lineFeed.claim().trim();
const lineWithoutStartingUnderscore = line.slice(1, line.length);
const lineEndsWithUnderscore = lineWithoutStartingUnderscore.endsWith("`");
if (lineEndsWithUnderscore) {
const lineWithoutUnderscores = lineWithoutStartingUnderscore.slice(
0,
lineWithoutStartingUnderscore.length - 1
);
instance.text = lineWithoutUnderscores;
return instance;
}
const endOfItalicString = lineWithoutStartingUnderscore.indexOf("`");
const lineWithoutUnderscores = lineWithoutStartingUnderscore.slice(
0,
endOfItalicString
);
const rest = lineWithoutStartingUnderscore.slice(endOfItalicString + 1);
lineFeed.push(rest);
instance.text = lineWithoutUnderscores;
return instance;
}
render() {
return `<code>${this.text}</code>`;
}
}
class MultiLineCode extends Symbol {
/**
* @type {string}
*/
text = "";
/**
*
* @param {string} line
* @returns {boolean}
*/
static canParse(line) {
const trimmedLine = line.trim();
return trimmedLine.startsWith("```");
}
/**
*
* @param {LineFeed} lineFeed
* @returns {Symbol}
*/
static create(lineFeed) {
const instance = new MultiLineCode();
const lines = [];
const firstLine = lineFeed.claim().trim();
const lineWithoutBackticks = line.slice(1, line.length - 1);
instance.text = lineWithoutBackticks;
return instance;
}
render() {
return `<code>${this.text}</code>`;
}
}
class CatchAll extends Symbol {
/**
* @type {string}
@@ -179,11 +301,11 @@ class CatchAll extends Symbol {
return instance;
}
render() {
return `<p>${this.text}</p>`;
return `<span>${this.text}</span>`;
}
}
const Factories = [Heading, Bold, Italic, CatchAll];
const Factories = [Heading, Bold, Italic, SingleLineCode, CatchAll];
export const toHtml = (markdown) => {
const symbols = toSymbols(markdown);

View File

@@ -35,10 +35,16 @@ Oh and I need some test markdown symbols to practice on..
_Italics_
_Italics_ then text
**Bold**
**Bold** then text
`Single line of code`
`code` then text
```
Multi
Line