Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
barista: Added transformer that removes content marked as internal fr…
Browse files Browse the repository at this point in the history
…om pages.
  • Loading branch information
Lara Aigmueller authored and thomaspink committed Jan 31, 2020
1 parent d35e246 commit 30214f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tools/barista/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { overviewBuilder } from './generators/category-navigation';
import {
internalLinksTransformerFactory,
exampleInlineSourcesTransformerFactory,
internalContentTransformerFactory,
} from './transform';

// Add your page-builder to this map to register it.
Expand All @@ -56,6 +57,15 @@ function createInternalLinksTransformer(): BaPageTransformer {
return internalLinksTransformerFactory(isPublic, internalLinkParts);
}

/**
* Creates the internalContentTransformer via a factory because we need to read
* one argument from the process environment.
*/
function createInternalContentTransformer(): BaPageTransformer {
const isPublic = isPublicBuild();
return internalContentTransformerFactory(isPublic);
}

/**
* Creates the exampleInlineSourcesTransformer by loading the example
* metadata-json and calling the factory with it.
Expand Down Expand Up @@ -83,6 +93,7 @@ async function buildPages(): Promise<void[]> {
const globalTransformers = [
await createExampleInlineSourcesTransformer(),
createInternalLinksTransformer(),
createInternalContentTransformer(),
];

const builders = Array.from(BUILDERS.values());
Expand Down
26 changes: 26 additions & 0 deletions tools/barista/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export function internalLinksTransformerFactory(
return transformed;
};
}

export function exampleInlineSourcesTransformerFactory(
metadata: BaAllExamplesMetadata,
): BaPageTransformer {
Expand All @@ -251,3 +252,28 @@ export function exampleInlineSourcesTransformerFactory(
return source;
};
}

/**
* Removes internal content wrapped with <ba-internal-content>
* from Strapi pages on public build.
*/
export function internalContentTransformerFactory(
isPublic: boolean,
): BaPageTransformer {
return async source => {
const transformed = { ...source };

transformed.content = runWithCheerio(source.content, ($: CheerioStatic) => {
$('ba-internal-content').each((_, content) => {
if (isPublic) {
$(content).remove();
} else {
const innerHtml = $(content).html();
$(content).replaceWith($(innerHtml));
}
});
});

return transformed;
};
}

0 comments on commit 30214f7

Please sign in to comment.