SimpleX-Chat/website/customize_docs_frontmatter.js
M Sarmad Qadeer f97a1fcedf
website: add docs to website (#2080)
* website: add fontmatter & improve image URLs where necessary

* website: add docs to website

* website: add prismjs for code highlighting

* website: change npm install position in web.sh

* website: fix an image URL in lang/cs/README.md

* website: improve image paths in lang/cs/translations.md

* website: add responsiveness & improve stylings of docs

* website: add dir to navbar in blog & docs

* website: remove scroll in mobile dropdown menu

* website: remove rfcs & add guide docs to website

* website: remove file renaming script from web.sh

* website: add menu to docs in nav

* website: add hash list & add scroll to headers

* website: customize docs frontmatter through JS

* website: remove supported_languages.json

* website: move merge_translations.js to JS folder

* website: add the following changes to docs
- add frontmatter to new doc merged from master
- add ignoreForWeb property to frontmatter of README.md docs

* website: remove package-lock.json from .gitignore

* website: add package-lock.json from .gitignore

* website: add no docs message to docs dropdown

* website: improve the sidebar of docs

* website: add revision date to docs

* website: add script to add version to docs frontmatter

* website: add layout to display message in docs if its version is old

* website: improve nav responsiveness

* website: remove frontmatter form main README & rfcs

* website: remove rfcs from website folder

* website: add ignore condition for rfcs in .eleventy

* website: remove frontmatter from lang README docs

* website: remove README from website's lang docs

* website: add guides menu in nav

* website: following changes
- add docs_dropdown.json
- extend reference menu in nav
- remove docs menu from nav

* website: fix in docs sidebar

* website: revert main docs README.md files

* website: revert main docs README.md files

* website: move scripts out of js that are for build

* website: remove displayAt form guide docs

* website: create a docs_sidebar.json & shift to that approach

* update navigation

* website: set navbar

* website: add icons to external links

* website: change the approach for docs sidebar creation

* website: update docs template

* website: add some strings to en.json and map them accordingly

* remove icon

---------

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
2023-04-30 22:31:23 +01:00

90 lines
3.7 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');
const directoryPath = path.resolve(__dirname, 'src/docs');
const langFolder = 'lang';
const enFiles = {};
function traverseDirectory(directory, currentLanguage = 'en', result = {}, callback) {
const filesAndDirectories = fs.readdirSync(directory);
filesAndDirectories.forEach((fileOrDirectoryName) => {
const fullPath = path.join(directory, fileOrDirectoryName);
if (fs.statSync(fullPath).isDirectory()) {
// If the subdirectory is inside the 'lang' folder, update the current language
if (directory.endsWith('/lang')) {
currentLanguage = fileOrDirectoryName;
}
// Recursively traverse the subdirectories
traverseDirectory(fullPath, currentLanguage, result, callback);
} else {
// Process the file only if it has the '.md' extension
if (path.extname(fullPath) === '.md') {
// Add the language to the file's language array or create a new array if it doesn't exist
const fileName = path.basename(fullPath, '.md');
if (!result[fileName]) {
result[fileName] = [];
}
result[fileName].push(currentLanguage);
}
if (callback) {
callback(fullPath, currentLanguage);
}
}
});
return result;
}
const fileLanguageMapping = traverseDirectory(directoryPath);
// Update the frontmatter of each Markdown file
Object.entries(fileLanguageMapping).forEach(([fileName, languages]) => {
// Find and update the frontmatter of each Markdown file
traverseDirectory(directoryPath, null, {}, (fullPath, currentLanguage) => {
if (path.basename(fullPath) === `${fileName}.md`) {
// Read the existing frontmatter
const fileContent = fs.readFileSync(fullPath, 'utf-8');
const parsedMatter = matter(fileContent);
const relativePath = path.relative(directoryPath, fullPath);
// Calculate the permalink based on the file's location
const linkPath = path.relative(directoryPath, fullPath).replace(/\.md$/, '.html');
const permalink = `/docs/${linkPath}`.toLowerCase();
parsedMatter.data.permalink = permalink;
// Update the frontmatter with the new languages list
parsedMatter.data.supportedLangsForDoc = languages;
// Add the layout value
parsedMatter.data.layout = 'layouts/doc.html';
if (fullPath.startsWith(path.join(directoryPath, langFolder))) {
// Non-English files
const [language, ...rest] = relativePath.split(path.sep).slice(1);
const enFilePath = path.join(directoryPath, ...rest);
if (enFiles[enFilePath]) {
const enRevision = new Date(enFiles[enFilePath].revision);
const currentRevision = new Date(parsedMatter.data.revision);
const isOld = currentRevision < enRevision;
// Add the version value
parsedMatter.data.version = isOld ? 'old' : 'new';
}
} else {
// English files
enFiles[fullPath] = { revision: parsedMatter.data.revision };
// Add the version value
parsedMatter.data.version = 'new';
}
// Save the updated frontmatter and content back to the file
const updatedFileContent = matter.stringify(parsedMatter.content, parsedMatter.data);
fs.writeFileSync(fullPath, updatedFileContent, 'utf-8');
}
});
});