Multi line code block support
This commit is contained in:
67
markdown.js
67
markdown.js
@@ -265,14 +265,51 @@ class MultiLineCode extends 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;
|
||||
const line = lineFeed.claim().trim();
|
||||
const lineWithoutStartTag = line.slice(3, line.length);
|
||||
|
||||
const sameLineContainsEnding = lineWithoutStartTag.includes("```");
|
||||
if (sameLineContainsEnding) {
|
||||
const endsWithTag = lineWithoutStartTag.endsWith("```");
|
||||
if (endsWithTag) {
|
||||
instance.text = lineWithoutStartTag.slice(
|
||||
0,
|
||||
lineWithoutStartTag.length - 3
|
||||
);
|
||||
return instance;
|
||||
}
|
||||
const [text, rest] = splitStringAtToken("```", lineWithoutStartTag);
|
||||
lineFeed.push(rest);
|
||||
instance.text = text;
|
||||
return instance;
|
||||
}
|
||||
const lines = [lineWithoutStartTag];
|
||||
|
||||
let endTokenFound = false;
|
||||
while (!endTokenFound && !lineFeed.isEmpty()) {
|
||||
const nextLine = lineFeed.claim();
|
||||
if (!nextLine.includes("```")) {
|
||||
lines.push(nextLine);
|
||||
continue;
|
||||
}
|
||||
if (nextLine.endsWith("```")) {
|
||||
lines.push(nextLine.slice(0, nextLine.length - 3));
|
||||
break;
|
||||
}
|
||||
const [lastLine, rest] = splitStringAtToken("```", nextLine);
|
||||
lineFeed.push(rest);
|
||||
lines.push(lastLine);
|
||||
break;
|
||||
}
|
||||
|
||||
instance.text = lines.join("\n");
|
||||
return instance;
|
||||
}
|
||||
render() {
|
||||
return `<code>${this.text}</code>`;
|
||||
return this.text
|
||||
.split("\n")
|
||||
.map((text) => `<code style="display: block;">${text}</code>`)
|
||||
.join("");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,7 +342,25 @@ class CatchAll extends Symbol {
|
||||
}
|
||||
}
|
||||
|
||||
const Factories = [Heading, Bold, Italic, SingleLineCode, CatchAll];
|
||||
/**
|
||||
*
|
||||
* @param {string} token
|
||||
* @param {string} string
|
||||
* @returns {string[]}
|
||||
*/
|
||||
const splitStringAtToken = (token, string) => {
|
||||
const index = string.indexOf(token);
|
||||
return [string.slice(0, index), string.slice(index + token.length)];
|
||||
};
|
||||
|
||||
const Factories = [
|
||||
Heading,
|
||||
Bold,
|
||||
Italic,
|
||||
MultiLineCode,
|
||||
SingleLineCode,
|
||||
CatchAll,
|
||||
];
|
||||
|
||||
export const toHtml = (markdown) => {
|
||||
const symbols = toSymbols(markdown);
|
||||
|
||||
@@ -21,6 +21,12 @@ But it's a start!
|
||||
|
||||
Oh and I need some test markdown symbols to practice on..
|
||||
|
||||
```
|
||||
codeblock
|
||||
codeblock
|
||||
|
||||
```
|
||||
|
||||
# H1
|
||||
|
||||
## H2
|
||||
|
||||
Reference in New Issue
Block a user