a simple keynote software for Markdown files – written in electron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 lines
2.4 KiB

"use strict";
const {Lexer, marked} = require("marked");
const util = require("util");
const tokenizer = {
list : (source) => {
console.log(source);
return false;
}
};
marked.use({tokenizer});
class Slide {
title;
content;
constructor(content = []) {
this.title = false;
this.content = content;
}
append(object) {
this.content.push(object);
}
}
const tokenize = string => {
//console.log(util.inspect(marked.Lexer.rules.block.listItemStart, true, null, true));
return new Lexer({gfm : true}).lex(string);
};
const injectTitle = (deck, meta) => {
const title = [];
if (meta?.icon)
title.push({
type : "image",
href : meta.icon
});
if (meta?.title)
title.push({
type : "heading",
level : 1,
tokens : [
{
type : "text",
text : meta.title
}
]
});
if (meta?.subtitle)
title.push({
type : "heading",
level : 2,
tokens : [
{
type : "text",
text : meta.subtitle
}
]
});
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}>`
},
]
});
if (title.length > 0) {
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
}