Skip to content

Commit

Permalink
Delete Graybox blocks during Promote (adobecom#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
arshadparwaiz authored Jun 10, 2024
1 parent d8758dd commit b167c4f
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions actions/docxUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DEFAULT_STYLES = require('../defaultstyles.xml');

const gbStyleExpression = 'gb-'; // graybox style expression. need to revisit if there are any more styles to be considered.
const emptyString = '';
const grayboxStylesRegex = new RegExp('gb-[a-zA-Z0-9,._-]*', 'g');
const grayboxStylesRegex = /gb-[a-zA-Z0-9,._-]*/g;
const gbDomainSuffix = '-graybox';
const logger = getAioLogger();
let firstGtRows = [];
Expand All @@ -41,10 +41,17 @@ async function updateDocument(content, expName, hlxAdminApiKey) {
const state = { content: { data: content }, log: '' };
await parseMarkdown(state);
const { mdast } = state.content;
updateExperienceNameFromLinks(mdast.children, expName);
const mdastChildren = mdast.children;

// Transform Graybox Links
updateExperienceNameFromLinks(mdastChildren, expName);

// Remove Graybox Styles
iterateGtRowsToReplaceStyles();

// Delete all Graybox Blocks in the document
iterateGtRowsToDeleteGrayboxBlock(mdastChildren);

try {
// generated docx file from updated mdast
docx = await generateDocxFromMdast(mdast, hlxAdminApiKey);
Expand Down Expand Up @@ -133,6 +140,49 @@ function findFirstGtRowInNode(node) {
return null;
}

/**
* Checks if the given node is a graybox block.
*/
const isGbBlock = (gtRowNode) => {
if (gtRowNode && gtRowNode.children) {
// eslint-disable-next-line no-restricted-syntax
for (const child of gtRowNode.children) {
if (child.type === 'text' && child.value && child.value.includes('graybox')) {
return true;
}
if (isGbBlock(child)) {
return true;
}
}
}
return false;
};

/**
* Find and delete all graybox blocks from the given mdast.
*/
const iterateGtRowsToDeleteGrayboxBlock = (mdastChildren) => {
try {
let blockCtr = -1;
const gbBlockIndexes = [];
mdastChildren.forEach((gtRow) => {
// Increment for each block
blockCtr += 1;
const isGrayboxBlock = isGbBlock(gtRow);
if (isGrayboxBlock) {
gbBlockIndexes.push(blockCtr);
}
});
let updatedGbIndexCtr = 0;
gbBlockIndexes.forEach((index) => {
mdastChildren.splice(index - updatedGbIndexCtr, 1);
updatedGbIndexCtr += 1;
});
} catch (err) {
logger.error(`Error while iterating GTRows to Delete Graybox Blocks ${err}`);
}
};

/**
* Generate a Docx file from the given mdast.
* @param {Object} mdast - The mdast representing the document.
Expand Down

0 comments on commit b167c4f

Please sign in to comment.