libre
Version:
World's lightest CMS
36 lines (27 loc) • 895 B
JavaScript
import _ from 'lodash';
export default class Item {
constructor({id, title, links, path}, libre) {
this.libre = libre;
_.assign(this, {id, title, path});
this.links = [];
if (links && links != '') {
const lines = links.split('\n');
lines.map(link => {
const pieces = link.split('|');
if (pieces.length >= 2) this.links.push({title: pieces[0], url: pieces[1]});
});
}
}
//for a given field, split into sections based on line breaks (\n)
//ignoring empty (double) breaks
//auto run Libre.process() on sections, so this will return a section array of piece arrays (nested array)
sections(field) {
const text = this[field];
if (!text) return [];
const sections = [];
text.split('\n').map(raw => {
if (raw.trim() != '') sections.push( this.libre.process(raw.trim()) );
});
return sections;
}
}