Rebuild to handle symbol not using whole line
This commit is contained in:
134
markdown.js
134
markdown.js
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user