100 lines
1.9 KiB
100 lines
1.9 KiB
"use strict";
|
|
|
|
const {lexer} = require("marked");
|
|
|
|
class Slide {
|
|
title;
|
|
content;
|
|
|
|
constructor(content = []) {
|
|
this.title = false;
|
|
this.content = content;
|
|
}
|
|
|
|
append(object) {
|
|
this.content.push(object);
|
|
}
|
|
}
|
|
|
|
const tokenize = string => {
|
|
return lexer(string);
|
|
};
|
|
|
|
const injectTitle = (deck, meta) => {
|
|
const title = [];
|
|
|
|
if (meta.title)
|
|
title.push({
|
|
type : "heading",
|
|
level : 1,
|
|
tokens : [
|
|
{
|
|
type : "text",
|
|
text : meta.title
|
|
}
|
|
]
|
|
});
|
|
if (meta.author)
|
|
title.push({
|
|
type : "paragraph",
|
|
tokens : [
|
|
{
|
|
type : "text",
|
|
text : `– ${meta.author}`
|
|
},
|
|
]
|
|
});
|
|
if (meta.email)
|
|
title.push({
|
|
type : "paragraph",
|
|
tokens : [
|
|
{
|
|
type : "text",
|
|
text : `<${meta.email}>`
|
|
},
|
|
]
|
|
});
|
|
const slide = new Slide(title);
|
|
slide.title = true;
|
|
deck.unshift(slide);
|
|
|
|
return deck;
|
|
}
|
|
|
|
const parser = string => {
|
|
const tokenStream = tokenize(string);
|
|
const slideDeck = [];
|
|
let currentSlide = new Slide();
|
|
let metaData = null;
|
|
|
|
for (const token of tokenStream) {
|
|
if ( token.type === "space" || (token.type === "heading" && token.depth === 1) || token.type === "html") {
|
|
if (currentSlide.content.length > 0) {
|
|
slideDeck.push(currentSlide);
|
|
currentSlide = new Slide();
|
|
}
|
|
if (!metaData && token.type === "html" && token.text.charAt(2) === '-') {
|
|
const metaString = token.text.replace(/(<!--|-->)/gi, "");
|
|
const meta = {};
|
|
for (const attribute of metaString.split(';')) {
|
|
const [key, value] = attribute.split(':');
|
|
meta[key.trim()] = value.trim();
|
|
}
|
|
metaData = meta;
|
|
}
|
|
|
|
if (token.type === "heading")
|
|
slideDeck.push(new Slide([token]));
|
|
continue;
|
|
}
|
|
currentSlide.append(token);
|
|
}
|
|
if (currentSlide.content.length > 0)
|
|
slideDeck.push(currentSlide);
|
|
|
|
return injectTitle(slideDeck, metaData);
|
|
};
|
|
|
|
module.exports = {
|
|
parser
|
|
} |