Skip to content

Commit

Permalink
Parser: Adds comments to attributes parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 9, 2017
1 parent 1124cfe commit 1800bba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { createBlock } from './factory';
* @return {Object} All block attributes
*/
export function getBlockAttributes( blockSettings, rawContent, attributes ) {
// The blockSettings.attributes contains the definition of each attribute
// depending on its "source", we retrieve its value from the comment attribute
// or by parsing the block content
const computedAttributes = reduce( blockSettings.attributes, ( memo, attribute, key ) => {
if ( attribute.source === 'metadata' ) {
memo[ key ] = attributes[ attribute.name || key ];
Expand Down
18 changes: 15 additions & 3 deletions blocks/api/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const addDescriptor = ( description ) => ( memo ) => {
return Object.assign( memo, description );
};

// Source descriptors
// Each one of these functions defines how to retrieve the attribute value
//
// - the descriptor sets "source: content" and a parse function for attributes parsed from block content
// - the descriptor sets "source: metadata" and an attribute name for attributes stored in the block comment
const attr = ( ...args ) => addDescriptor( { source: 'content', parse: originalAttr( ...args ) } );
const prop = ( ...args ) => addDescriptor( { source: 'content', parse: originalProp( ...args ) } );
const html = ( ...args ) => addDescriptor( { source: 'content', parse: originalHtml( ...args ) } );
Expand All @@ -44,13 +49,20 @@ const query = ( selector, descriptor ) => {
} );
};

const accumulateOn = ( description ) => {
/**
* Takes an argument description and returns a chainable API to describe the current attribute
*
* @param {?Object} description The argument description
*
* @return {Object} descriptors chainable API
*/
const getChainableAPI = ( description ) => {
return reduce( { attr, prop, html, text, query, children, metadata }, ( memo, fct, key ) => {
const wrappedFct = ( ...args ) => {
const accumulator = fct( ...args );
const newDescription = accumulator( description || {} );
return {
...accumulateOn( newDescription ),
...getChainableAPI( newDescription ),
__description: newDescription
};
};
Expand All @@ -60,4 +72,4 @@ const accumulateOn = ( description ) => {
}, {} );
};

export default accumulateOn();
export default getChainableAPI();

0 comments on commit 1800bba

Please sign in to comment.