Bold and italic support
This commit is contained in:
66
markdown.js
66
markdown.js
@@ -90,6 +90,70 @@ class Heading extends Symbol {
|
||||
}
|
||||
}
|
||||
|
||||
class Italic extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
text = "";
|
||||
/**
|
||||
*
|
||||
* @param {string} line
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
const trimmedLine = line.trim();
|
||||
return trimmedLine.startsWith("_") && trimmedLine.endsWith("_");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {LineFeed} lineFeed
|
||||
* @returns {Symbol}
|
||||
*/
|
||||
static create(lineFeed) {
|
||||
const instance = new Italic();
|
||||
const line = lineFeed.claim().trim();
|
||||
const lineWithoutUnderscores = line.slice(1, line.length - 1);
|
||||
instance.text = lineWithoutUnderscores;
|
||||
return instance;
|
||||
}
|
||||
render() {
|
||||
return `<i>${this.text}</i>`;
|
||||
}
|
||||
}
|
||||
|
||||
class Bold extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
text = "";
|
||||
/**
|
||||
*
|
||||
* @param {string} line
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static canParse(line) {
|
||||
const trimmedLine = line.trim();
|
||||
return trimmedLine.startsWith("**") && trimmedLine.endsWith("**");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {LineFeed} lineFeed
|
||||
* @returns {Symbol}
|
||||
*/
|
||||
static create(lineFeed) {
|
||||
const instance = new Bold();
|
||||
const line = lineFeed.claim().trim();
|
||||
const lineWithoutUnderscores = line.slice(2, line.length - 2);
|
||||
instance.text = lineWithoutUnderscores;
|
||||
return instance;
|
||||
}
|
||||
render() {
|
||||
return `<b>${this.text}</b>`;
|
||||
}
|
||||
}
|
||||
|
||||
class CatchAll extends Symbol {
|
||||
/**
|
||||
* @type {string}
|
||||
@@ -119,7 +183,7 @@ class CatchAll extends Symbol {
|
||||
}
|
||||
}
|
||||
|
||||
const Factories = [Heading, CatchAll];
|
||||
const Factories = [Heading, Bold, Italic, CatchAll];
|
||||
|
||||
export const toHtml = (markdown) => {
|
||||
const symbols = toSymbols(markdown);
|
||||
|
||||
Reference in New Issue
Block a user