Skip to content

Commit

Permalink
Allow to set slot separator via options
Browse files Browse the repository at this point in the history
  • Loading branch information
thewebartisan7 committed Oct 23, 2022
1 parent 39e7481 commit 77e8f62
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = (options = {}) => tree => {
options.yield = options.yield || 'yield';
options.slot = options.slot || 'slot';
options.fill = options.fill || 'fill';
options.slotSeparator = options.slotSeparator || ':';
options.push = options.push || 'push';
options.stack = options.stack || 'stack';
options.localsAttr = options.localsAttr || 'props';
Expand All @@ -42,11 +43,11 @@ module.exports = (options = {}) => tree => {
options.strict = typeof options.strict === 'undefined' ? true : options.strict;

if (!(options.slot instanceof RegExp)) {
options.slot = new RegExp(`^${options.slot}:`, 'i');
options.slot = new RegExp(`^${options.slot}${options.slotSeparator}`, 'i');
}

if (!(options.fill instanceof RegExp)) {
options.fill = new RegExp(`^${options.fill}:`, 'i');
options.fill = new RegExp(`^${options.fill}${options.slotSeparator}`, 'i');
}

if (!(options.tagPrefix instanceof RegExp)) {
Expand Down
23 changes: 12 additions & 11 deletions src/slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ const {match} = require('posthtml/lib/api');
const {render} = require('posthtml-render');
const {each, omit} = require('underscore');

const separator = ':';

/**
* Set filled slots
*
* @param {Object} currentNode PostHTML tree
* @param {Object} filledSlots
* @param {Object} options Plugin options
* @param {String} fill Fill tag name
* @param {String} slotSeparator Slot separator
* @return {void}
*/
function setFilledSlots(currentNode, filledSlots, {fill}) {
function setFilledSlots(currentNode, filledSlots, {fill, slotSeparator}) {
match.call(currentNode, {tag: fill}, fillNode => {
if (!fillNode.attrs) {
fillNode.attrs = {};
}

const name = fillNode.tag.split(separator)[1];
const name = fillNode.tag.split(slotSeparator)[1];

const locals = omit(fillNode.attrs, [name, 'type', 'append', 'prepend', 'aware']);

Expand Down Expand Up @@ -51,12 +50,13 @@ function setFilledSlots(currentNode, filledSlots, {fill}) {
*
* @param {Object} tree PostHTML tree
* @param {Object} filledSlots Filled slots content
* @param {Object} options Plugin options
* @param {String} fill Fill tag name
* @param {String} slotSeparator Slot separator
* @return {void}
*/
function processFillContent(tree, filledSlots, {fill}) {
function processFillContent(tree, filledSlots, {fill, slotSeparator}) {
match.call(tree, {tag: fill}, fillNode => {
const name = fillNode.tag.split(separator)[1];
const name = fillNode.tag.split(slotSeparator)[1];

if (!filledSlots[name]) {
filledSlots[name] = {};
Expand All @@ -80,12 +80,13 @@ function processFillContent(tree, filledSlots, {fill}) {
*
* @param {Object} tree PostHTML tree
* @param {Object} filledSlots Filled slots content
* @param {Object} options Plugin options
* @param {String} slot Slot tag name
* @param {String} slotSeparator Slot separator
* @return {void}
*/
function processSlotContent(tree, filledSlots, {slot}) {
function processSlotContent(tree, filledSlots, {slot, slotSeparator}) {
match.call(tree, {tag: slot}, slotNode => {
const name = slotNode.tag.split(separator)[1];
const name = slotNode.tag.split(slotSeparator)[1];

slotNode.tag = false;

Expand Down

0 comments on commit 77e8f62

Please sign in to comment.