From 1cfe5982bb4c213bf3067257015d6e43ccd2259a Mon Sep 17 00:00:00 2001 From: Damir Date: Tue, 18 Oct 2022 13:39:16 +0200 Subject: [PATCH] Push once and prepend --- src/index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3a24192..4e22477 100644 --- a/src/index.js +++ b/src/index.js @@ -231,11 +231,24 @@ function setFilledSlots(currentNode, slots, {slot}) { */ function processPushes(tree, content, {push}) { match.call(tree, {tag: push}, pushNode => { + if (!pushNode.attrs || !pushNode.attrs.name) { + throw new Error(`[components] Push <${push}> tag must have an attribute "name".`); + } + if (!content[pushNode.attrs.name]) { content[pushNode.attrs.name] = []; } - content[pushNode.attrs.name].push(pushNode.content); + const pushContent = render(pushNode.content); + + if (typeof pushNode.attrs.once === 'undefined' || !content[pushNode.attrs.name].includes(pushContent)) { + if (typeof pushNode.attrs.prepend === 'undefined') { + content[pushNode.attrs.name].push(pushContent); + } else { + content[pushNode.attrs.name].unshift(pushContent); + } + } + pushNode.tag = false; pushNode.content = null;