diff --git a/.changeset/rare-crabs-cheat.md b/.changeset/rare-crabs-cheat.md new file mode 100644 index 0000000000..6b4fd37ed7 --- /dev/null +++ b/.changeset/rare-crabs-cheat.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs': patch +--- + +Add support for mkdocs material palette conditional hashes. diff --git a/plugins/techdocs/src/reader/transformers/styles/rules/palette.ts b/plugins/techdocs/src/reader/transformers/styles/rules/palette.ts new file mode 100644 index 0000000000..fa1f69ac88 --- /dev/null +++ b/plugins/techdocs/src/reader/transformers/styles/rules/palette.ts @@ -0,0 +1,38 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RuleOptions } from './types'; +import { Theme } from '@material-ui/core/styles'; + +const themeHashes: Record> = { + dark: ['#only-light', '#gh-light-mode-only'], + light: ['#only-dark', '#gh-dark-mode-only'], +}; + +export default ({ theme }: RuleOptions) => ` +/*================== Palette ==================*/ +/* + When color palette toggle is enabled in material theme for Mkdocs, there is a possibility to show conditionally + images by adding #only-dark or #only-light to resource hash. Backstage doesn't use mkdocs color palette mechanism, + so there is a need to add css rules from palette*.css manually. +*/ + +${themeHashes[theme.palette.type] + .map(hash => `img[src$="${hash}"]`) + .join(', ')} { + display: none; +} +`; diff --git a/plugins/techdocs/src/reader/transformers/styles/rules/rules.ts b/plugins/techdocs/src/reader/transformers/styles/rules/rules.ts index 917793733e..9341fd6e3e 100644 --- a/plugins/techdocs/src/reader/transformers/styles/rules/rules.ts +++ b/plugins/techdocs/src/reader/transformers/styles/rules/rules.ts @@ -20,6 +20,7 @@ import { default as layout } from './layout'; import { default as typeset } from './typeset'; import { default as animations } from './animations'; import { default as extensions } from './extensions'; +import palette from './palette'; /** * A list of style rules that will be applied to an element in the order they were added. @@ -35,4 +36,5 @@ export const rules = [ typeset, animations, extensions, + palette, ]; diff --git a/plugins/techdocs/src/reader/transformers/styles/transformer.test.tsx b/plugins/techdocs/src/reader/transformers/styles/transformer.test.tsx index b55e372212..6cfde4f09d 100644 --- a/plugins/techdocs/src/reader/transformers/styles/transformer.test.tsx +++ b/plugins/techdocs/src/reader/transformers/styles/transformer.test.tsx @@ -47,6 +47,9 @@ describe('Transformers > Styles', () => { expect(style).toHaveTextContent( '/*================== Extensions ==================*/', ); + expect(style).toHaveTextContent( + '/*================== Palette ==================*/', + ); }); it('should use headers relative font-size value as the factor for the md-typeset variable', () => {