refactored meta data parsing

now not requiring a `;` at the end of the lines
master
Michael Ochmann 2 years ago
parent f26603df3a
commit ef8a1b0c0d
  1. 55
      src/Parser.js

@ -28,13 +28,32 @@ class Slide {
} }
} }
class SlideDeck {
metaData;
slides;
constructor() {
this.metaData = null;
this.slides = [];
}
push(slide) {
this.slides.push(slide);
}
unshift(slide) {
this.slides.unshift(slide);
}
}
const tokenize = string => { const tokenize = string => {
//console.log(util.inspect(marked.Lexer.rules.block.listItemStart, true, null, true)); //console.log(util.inspect(marked.Lexer.rules.block.listItemStart, true, null, true));
return new Lexer({gfm : true}).lex(string); return new Lexer({gfm : true}).lex(string);
}; };
const injectTitle = (deck, meta) => { const injectTitle = (deck) => {
const title = []; const title = [];
const meta = deck.metaData;
if (meta?.icon) if (meta?.icon)
title.push({ title.push({
@ -93,11 +112,29 @@ const injectTitle = (deck, meta) => {
return deck; return deck;
} }
const parseMetaData = source => {
const meta = {};
for (const attribute of source.split('\n')) {
const trimmed = attribute.trim();
if (trimmed.length < 1 || (trimmed.charAt(0) === '/' && trimmed.charAt(1) === '/'))
continue;
const [key, value] = trimmed.split(':');
if (key.trim().length < 1 || value.trim().length < 1)
continue;
meta[key.trim()] = value.trim();
}
console.table(meta);
return meta;
};
const parser = string => { const parser = string => {
const tokenStream = tokenize(string); const tokenStream = tokenize(string);
const slideDeck = []; const slideDeck = new SlideDeck();
let currentSlide = new Slide(); let currentSlide = new Slide();
let metaData = null;
for (const token of tokenStream) { for (const token of tokenStream) {
if (token.type === "space" || (token.type === "heading" && token.depth === 1) || token.type === "html") { if (token.type === "space" || (token.type === "heading" && token.depth === 1) || token.type === "html") {
@ -105,14 +142,10 @@ const parser = string => {
slideDeck.push(currentSlide); slideDeck.push(currentSlide);
currentSlide = new Slide(); currentSlide = new Slide();
} }
if (!metaData && token.type === "html" && token.text.charAt(2) === '-') { // basically, if this is the forst comment, we expect meta data
if (!slideDeck.metaData && token.type === "html" && token.text.charAt(2) === '-') {
const metaString = token.text.replace(/(<!--|-->)/gi, ""); const metaString = token.text.replace(/(<!--|-->)/gi, "");
const meta = {}; slideDeck.metaData = parseMetaData(metaString);
for (const attribute of metaString.split(';')) {
const [key, value] = attribute.split(':');
meta[key.trim()] = value.trim();
}
metaData = meta;
} }
if (token.type === "heading") if (token.type === "heading")
@ -124,7 +157,7 @@ const parser = string => {
if (currentSlide.content.length > 0) if (currentSlide.content.length > 0)
slideDeck.push(currentSlide); slideDeck.push(currentSlide);
return injectTitle(slideDeck, metaData); return injectTitle(slideDeck);
}; };
module.exports = { module.exports = {

Loading…
Cancel
Save