From a7bfc936be6b90ee529a0b0c6634c548d058044f Mon Sep 17 00:00:00 2001 From: Julien Ramboz Date: Fri, 14 Jun 2019 16:26:43 -0700 Subject: [PATCH] refactor(html pipe): some linting fix #279 --- src/html/get-metadata.js | 31 ++++++++--------- src/html/split-sections.js | 19 +++++++++-- src/utils/mdast-to-vdom.js | 2 ++ src/utils/section-handler.js | 65 ++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 src/utils/section-handler.js diff --git a/src/html/get-metadata.js b/src/html/get-metadata.js index fb5938c6a..971f9120b 100644 --- a/src/html/get-metadata.js +++ b/src/html/get-metadata.js @@ -19,13 +19,14 @@ const { function yaml(section) { const yamls = selectAll('yaml', section); - section.meta = obj(flat(map(yamls, ({ payload }) => payload))); + section.data = section.data || {}; + section.data.meta = obj(flat(map(yamls, ({ payload }) => payload))); return section; } function title(section) { const header = select('heading', section); - section.title = header ? plain(header) : ''; + section.data.title = header ? plain(header) : ''; } function intro(section) { @@ -36,7 +37,7 @@ function intro(section) { } return true; })[0]; - section.intro = para ? plain(para) : ''; + section.data.intro = para ? plain(para) : ''; } function image(section) { @@ -44,7 +45,7 @@ function image(section) { // TODO: get a better measure of prominence than "first" const img = select('image', section); if (img) { - section.image = img.url; + section.data.image = img.url; } } @@ -132,35 +133,35 @@ function sectiontype(section) { } const typecounter = children.reduce(reducer, {}); - section.types = constructTypes(typecounter); + section.data.types = constructTypes(typecounter); } function fallback(section) { if (section.intro && !section.title) { - section.title = section.intro; + section.data.title = section.intro; } else if (section.title && !section.intro) { - section.intro = section.title; + section.data.intro = section.title; } } function getmetadata({ content }, { logger }) { - const { sections } = content; - if (!sections) { + const { mdast: { children } } = content; + if (!children) { content.meta = {}; return; } - logger.debug(`Parsing Markdown Metadata from ${sections.length} sections`); + logger.debug(`Parsing Markdown Metadata from ${children.length} sections`); each([yaml, title, intro, image, sectiontype, fallback], (fn) => { - each(sections, fn); + each(children, fn); }); - const img = sections.filter(section => section.image)[0]; + const img = children.filter(section => section.image)[0]; - content.meta = empty(sections) ? {} : sections[0].meta; - content.title = empty(sections) ? '' : sections[0].title; - content.intro = empty(sections) ? '' : sections[0].intro; + content.meta = empty(children) ? {} : children[0].meta; + content.title = empty(children) ? '' : children[0].title; + content.intro = empty(children) ? '' : children[0].intro; content.image = img ? img.image : undefined; } diff --git a/src/html/split-sections.js b/src/html/split-sections.js index 7a0f3a06a..c7aa882a4 100644 --- a/src/html/split-sections.js +++ b/src/html/split-sections.js @@ -11,8 +11,15 @@ */ const between = require('unist-util-find-all-between'); +const { selectAll } = require('unist-util-select'); +const { flat, obj, map } = require('@adobe/helix-shared').sequence; const _ = require('lodash/fp'); +// Compute the meta information for the section +function computeMeta(section) { + return obj(flat(map(selectAll('yaml', section), ({ payload }) => payload))); +} + function split({ content }) { const { mdast } = content; @@ -25,18 +32,26 @@ function split({ content }) { // include the very start and end of the document const starts = [0, ...dividers]; const ends = [...dividers, mdast.children.length]; - content.sections = _.zip(starts, ends) + + content.mdast.children = _.zip(starts, ends) // but filter out empty section .filter(([start, end]) => start !== end) // then return all nodes that are in between .map(([start, end]) => { // skip 'thematicBreak' nodes const index = mdast.children[start].type === 'thematicBreak' ? start + 1 : start; - return { + const section = { type: 'root', + data: {}, children: between(mdast, index, end), }; + section.data.meta = computeMeta(section); + return section; }); + + if (content.mdast.children.length === 1) { // unwrap sole section + content.mdast.children = content.mdast.children[0].children; + } } module.exports = split; diff --git a/src/utils/mdast-to-vdom.js b/src/utils/mdast-to-vdom.js index 0b48336b9..7680511fc 100644 --- a/src/utils/mdast-to-vdom.js +++ b/src/utils/mdast-to-vdom.js @@ -20,6 +20,7 @@ const HeadingHandler = require('./heading-handler'); const image = require('./image-handler'); const embed = require('./embed-handler'); const link = require('./link-handler'); +const section = require('./section-handler'); const types = require('../schemas/mdast.schema.json').properties.type.enum; /** @@ -62,6 +63,7 @@ class VDOMTransformer { this.match('image', image(options)); this.match('embed', embed(options)); this.match('link', link(options)); + this.match('root', section(options)); this.match('html', (h, node) => { if (node.value.startsWith('