@@ -5,6 +5,8 @@ import { fileURLToPath } from 'node:url'
55
66import { findUp , pathExists } from 'find-up'
77import glob from 'glob'
8+ import postcss from 'postcss'
9+ import postcssRtlCss from 'postcss-rtlcss'
810import semverCompare from 'semver-compare'
911
1012import { Assets } from './assets'
@@ -67,8 +69,19 @@ export async function buildDocs(options: BuildOptions): Promise<void> {
6769 * @param enContext The base (English) context.
6870 */
6971async function buildLangs ( enContext : Context ) : Promise < void > {
70- // Process all pages for each supported language
72+ // Process the CSS files to automatically generate RTL styles
7173 const config = enContext . config
74+ const cssFiles : Map < string , string > = new Map ( )
75+ function processCss ( sourceDir : string , fileName : string ) : void {
76+ const cssPath = resolvePath ( sourceDir , fileName )
77+ const srcCssContent = readTextFile ( cssPath )
78+ const outCssContent = postcss ( [ postcssRtlCss ( ) ] ) . process ( srcCssContent ) . css
79+ cssFiles . set ( fileName , outCssContent )
80+ }
81+ processCss ( config . sourceDir , 'base.css' )
82+ processCss ( config . baseProjDir , 'project.css' )
83+
84+ // Process all pages for each supported language
7285 const localizationDir = resolvePath ( config . baseProjDir , 'localization' )
7386 const langConfigs : LangConfig [ ] = [ ]
7487 langConfigs . push ( { code : 'en' , version : config . version } )
@@ -88,7 +101,7 @@ async function buildLangs(enContext: Context): Promise<void> {
88101
89102 // Build the docs for this language
90103 try {
91- await buildLang ( context , langConfig )
104+ await buildLang ( context , langConfig , cssFiles )
92105
93106 // Generate `en/docs.po`, which contains the base English strings.
94107 // We only need to generate this if this project is translated (has
@@ -109,8 +122,13 @@ async function buildLangs(enContext: Context): Promise<void> {
109122 *
110123 * @param context The language-specific context.
111124 * @param langConfig The version configuration for the language.
125+ * @param cssFiles The map of CSS files to be copied to the output directory.
112126 */
113- async function buildLang ( context : Context , langConfig : LangConfig ) : Promise < void > {
127+ async function buildLang (
128+ context : Context ,
129+ langConfig : LangConfig ,
130+ cssFiles : Map < string , string >
131+ ) : Promise < void > {
114132 // Check the version of the translation for this language
115133 const baseVersion = context . config . version
116134 let useSavedVersion : boolean
@@ -166,16 +184,21 @@ async function buildLang(context: Context, langConfig: LangConfig): Promise<void
166184 copyToBase ( await moduleDir ( 'lunr-languages' ) , 'lunr.stemmer.support.js' )
167185 copyToBase ( await moduleDir ( 'mark.js' , 'dist' ) , 'mark.min.js' )
168186
169- // Copy project-specific CSS
170- copyToBase ( context . config . baseProjDir , 'project.css' )
187+ // Write the preprocessed CSS files
188+ function writeCssFile ( fileName : string ) : void {
189+ const cssContent = cssFiles . get ( fileName )
190+ assets . writeWithHash ( cssContent , fileName , context . outDir )
191+ }
192+ writeCssFile ( 'base.css' )
193+ writeCssFile ( 'project.css' )
171194
172195 // Copy all other assets from the "shared src" directory. Note that glob paths
173196 // have forward slashes only, so convert backslashes here.
174197 const sharedSrcPath = context . config . sourceDir . replaceAll ( '\\' , '/' )
175198 const sharedSrcFiles = glob . sync ( `${ sharedSrcPath } /*` , { nodir : true } )
176199 for ( const f of sharedSrcFiles ) {
177200 const relPath = f . replace ( `${ sharedSrcPath } /` , '' )
178- if ( ! relPath . endsWith ( '.html' ) ) {
201+ if ( ! relPath . endsWith ( '.html' ) && ! relPath . endsWith ( 'base.css' ) ) {
179202 copyToBase ( context . config . sourceDir , relPath )
180203 }
181204 }
0 commit comments