-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use default language in config if arg not specified - post doc:gen fixes
- Loading branch information
1 parent
57bfbe4
commit 7b3612b
Showing
4 changed files
with
98 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,68 @@ | ||
import * as Cheerio from "cheerio"; | ||
import fs from 'fs' | ||
import fs from "fs"; | ||
import logger from "../utils/logger"; | ||
import path from "path"; | ||
import CONFIG from "../config"; | ||
|
||
export async function fixDuplicateLanguageReferences(languages: string[]) { | ||
export async function fixDuplicateLanguageReferences() { | ||
const languages = CONFIG.languages; | ||
// Get the index files for each language | ||
for (const lang of languages.slice(1)) { | ||
const directoryForHtmlFiles = path.join(CONFIG.outputDirectory, `_book/${lang}/`); | ||
const directoryForHtmlFiles = path.join( | ||
CONFIG.outputDirectory, | ||
`_book/${lang}/`, | ||
); | ||
|
||
logger.info('Reading index file for language: ' + lang) | ||
const indexFile = await fs.promises.readFile(directoryForHtmlFiles + `index.${lang}.html`, "utf-8"); | ||
logger.info('Finished reading index file for language: ' + lang) | ||
logger.info("Reading index file for language: " + lang); | ||
const indexFile = await fs.promises.readFile( | ||
directoryForHtmlFiles + `index.${lang}.html`, | ||
"utf-8", | ||
); | ||
logger.info("Finished reading index file for language: " + lang); | ||
|
||
// Get the html elements (anchor elements) with classname 'dropdown-item' from each index file | ||
const $ = Cheerio.load(indexFile); | ||
const dropdownItems = $('.dropdown-item'); | ||
const dropdownItems = $(".dropdown-item"); | ||
const uniqueItems = new Set(); | ||
|
||
// Remove duplicate elements from index file (use the href as the unique idendifier) | ||
dropdownItems.each((index, element) => { | ||
logger.info('Removing duplicate items from index file for language: ' + lang) | ||
const href = $(element).attr('href'); | ||
logger.info( | ||
"Removing duplicate items from index file for language: " + | ||
lang, | ||
); | ||
const href = $(element).attr("href"); | ||
if (uniqueItems.has(href)) { | ||
$(element).remove(); | ||
} | ||
uniqueItems.add(href); | ||
}); | ||
|
||
await fs.promises.writeFile(directoryForHtmlFiles + `index.${lang}.html`, $.html()); | ||
logger.info('Finished removing duplicate items from index file for language: ' + lang) | ||
await fs.promises.writeFile( | ||
directoryForHtmlFiles + `index.${lang}.html`, | ||
$.html(), | ||
); | ||
logger.info( | ||
"Finished removing duplicate items from index file for language: " + | ||
lang, | ||
); | ||
} | ||
} | ||
|
||
|
||
if (require.main === module) { | ||
const langs = process.argv | ||
.find((arg) => arg.startsWith("languages")) | ||
?.split("=")[1] | ||
?.split(","); | ||
|
||
if (!langs) { | ||
console.log( | ||
"Please provide languages to create localized docs for using the languages flag", | ||
console.warn( | ||
"Languages not specified in cli arguments, setting languages to default", | ||
); | ||
process.exit(1); | ||
} | ||
|
||
fixDuplicateLanguageReferences(langs) | ||
} | ||
CONFIG.languages = langs ?? CONFIG.languages; | ||
|
||
fixDuplicateLanguageReferences(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters