Skip to content

Commit

Permalink
[MegaLinter] Apply linters fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wesley-dean-gsa committed Dec 18, 2024
1 parent 496acc1 commit 61b19d6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
uswdsIcon,
imageWithClassShortcode,
} = require("./js/global.js");
const { validateMarkdownFiles } = require("./js/validateMarkdownFiles.js")
const { validateMarkdownFiles } = require("./js/validateMarkdownFiles.js");

require("dotenv").config();

Expand Down Expand Up @@ -183,7 +183,7 @@ module.exports = function (config) {
});

// Validate markdown files before build
config.on('beforeBuild', () => {
config.on("beforeBuild", () => {
validateMarkdownFiles(); // Call our validation function
});

Expand Down
36 changes: 32 additions & 4 deletions _schemas/job-posting.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,24 @@
},
"gs": {
"type": ["string", "integer", "null"],
"enum": ["SES", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"],
"enum": [
"SES",
"15",
"14",
"13",
"12",
"11",
"10",
"9",
"8",
"7",
"6",
"5",
"4",
"3",
"2",
"1"
],
"description": "The GS grade or SES level."
},
"travel_requirement": {
Expand Down Expand Up @@ -117,9 +134,20 @@
"items": {
"type": "object",
"properties": {
"link": { "type": "string", "format": "uri", "description": "Link to the info session." },
"date": { "type": "string", "format": "date", "description": "Date of the info session." },
"time": { "type": "string", "description": "Time of the info session in ET." }
"link": {
"type": "string",
"format": "uri",
"description": "Link to the info session."
},
"date": {
"type": "string",
"format": "date",
"description": "Date of the info session."
},
"time": {
"type": "string",
"description": "Time of the info session in ET."
}
},
"required": ["link", "date", "time"]
},
Expand Down
16 changes: 9 additions & 7 deletions js/redirects.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const fs = require('fs');
const path = require('path');
const fs = require("fs");
const path = require("path");

// Directory where markdown files are stored
const archiveDir = './pages/jointts/positions/archive';
const archiveDir = "./pages/jointts/positions/archive";

// Get all markdown files in the directory
const mdFiles = fs.readdirSync(archiveDir).filter(file => file.endsWith('.md'));
const mdFiles = fs
.readdirSync(archiveDir)
.filter((file) => file.endsWith(".md"));

// Map each markdown file to a redirect
module.exports = mdFiles.map(file => {
module.exports = mdFiles.map((file) => {
return {
permalink: `/pages/jointts/positions/archive/${file.replace('.md', '.html')}`, // The original page URL
redirectTo: '/join/', // The target for the redirect
permalink: `/pages/jointts/positions/archive/${file.replace(".md", ".html")}`, // The original page URL
redirectTo: "/join/", // The target for the redirect
};
});
121 changes: 63 additions & 58 deletions js/validateMarkdownFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,86 @@ const { execSync } = require("child_process");
const grayMatter = require("gray-matter");

function validateMarkdownFiles() {
const mdFilesDir = './pages/jointts/positions/'; // Directory with markdown files
const mdFiles = fs.readdirSync(mdFilesDir).filter(file => file.endsWith('.md') && file !== 'position-template.md'); // Skip 'position-template.md'
const mdFilesDir = "./pages/jointts/positions/"; // Directory with markdown files
const mdFiles = fs
.readdirSync(mdFilesDir)
.filter((file) => file.endsWith(".md") && file !== "position-template.md"); // Skip 'position-template.md'

const schemaPath = './_schemas/job-posting.json'; // Path to schema
const schemaPath = "./_schemas/job-posting.json"; // Path to schema

mdFiles.forEach(file => {
const filePath = path.join(mdFilesDir, file);
console.log(`Validating ${filePath}...`);
mdFiles.forEach((file) => {
const filePath = path.join(mdFilesDir, file);
console.log(`Validating ${filePath}...`);

try {
// Read and parse the markdown file's front matter and content
const fileContent = fs.readFileSync(filePath, 'utf8');
const parsed = grayMatter(fileContent);

// Convert the front matter into a JSON object
const dataToValidate = parsed.data;
try {
// Read and parse the markdown file's front matter and content
const fileContent = fs.readFileSync(filePath, "utf8");
const parsed = grayMatter(fileContent);

// Ensure the properties match the expected structure
// Convert the front matter into a JSON object
const dataToValidate = parsed.data;

// Ensure tags is an array
if (typeof dataToValidate.tags === 'string') {
dataToValidate.tags = [dataToValidate.tags]; // Convert string to array
}
// Ensure the properties match the expected structure

// Ensure tags is an array
if (typeof dataToValidate.tags === "string") {
dataToValidate.tags = [dataToValidate.tags]; // Convert string to array
}

// Ensure key_objectives is an array of objects with title and items
if (Array.isArray(dataToValidate.key_objectives)) {
dataToValidate.key_objectives = dataToValidate.key_objectives.map(obj => {
// Ensure key_objectives is an array of objects with title and items
if (Array.isArray(dataToValidate.key_objectives)) {
dataToValidate.key_objectives = dataToValidate.key_objectives.map(
(obj) => {
if (!obj.items) {
obj.items = [];
obj.items = [];
}
return obj;
});
},
);
}

// Ensure date fields are in correct format
const dateFields = ["opens", "closes", "updated"];
dateFields.forEach((field) => {
if (dataToValidate[field]) {
const date = new Date(dataToValidate[field]);
dataToValidate[field] = date.toISOString().split("T")[0]; // Convert to YYYY-MM-DD
}
});

// Ensure date fields are in correct format
const dateFields = ['opens', 'closes', 'updated'];
dateFields.forEach(field => {
if (dataToValidate[field]) {
const date = new Date(dataToValidate[field]);
dataToValidate[field] = date.toISOString().split('T')[0]; // Convert to YYYY-MM-DD
}
// Format the date in info_sessions to only keep the date part (YYYY-MM-DD)
if (Array.isArray(dataToValidate.info_sessions)) {
dataToValidate.info_sessions.forEach((session) => {
if (session.date) {
const date = new Date(session.date);
session.date = date.toISOString().split("T")[0]; // Keep only YYYY-MM-DD
}
});
}

// Format the date in info_sessions to only keep the date part (YYYY-MM-DD)
if (Array.isArray(dataToValidate.info_sessions)) {
dataToValidate.info_sessions.forEach(session => {
if (session.date) {
const date = new Date(session.date);
session.date = date.toISOString().split('T')[0]; // Keep only YYYY-MM-DD
}
});
}

// Set the permalink field based on the title field (lowercase and replace spaces with dashes)
if (dataToValidate.title) {
const permalink = `/join/${dataToValidate.title.toLowerCase().replace(/\s+/g, '-').replace(':', '')}.html`;
dataToValidate.permalink = permalink;
}
// Set the permalink field based on the title field (lowercase and replace spaces with dashes)
if (dataToValidate.title) {
const permalink = `/join/${dataToValidate.title.toLowerCase().replace(/\s+/g, "-").replace(":", "")}.html`;
dataToValidate.permalink = permalink;
}

// Create temporary JSON for validation
const tempJsonPath = '../tts.gsa.gov/temp.json';
fs.writeFileSync(tempJsonPath, JSON.stringify(dataToValidate, null, 2));
// Create temporary JSON for validation
const tempJsonPath = "../tts.gsa.gov/temp.json";
fs.writeFileSync(tempJsonPath, JSON.stringify(dataToValidate, null, 2));

// Run v8r validation
execSync(`npx v8r --schema ${schemaPath} ${tempJsonPath}`, { stdio: 'inherit' });
// Run v8r validation
execSync(`npx v8r --schema ${schemaPath} ${tempJsonPath}`, {
stdio: "inherit",
});

// Clean up temporary JSON file after validation
fs.unlinkSync(tempJsonPath);
} catch (error) {
console.error(`Validation failed for ${filePath}: ${error.message}`);
}
});
// Clean up temporary JSON file after validation
fs.unlinkSync(tempJsonPath);
} catch (error) {
console.error(`Validation failed for ${filePath}: ${error.message}`);
}
});
}

module.exports = {
validateMarkdownFiles
validateMarkdownFiles,
};

0 comments on commit 61b19d6

Please sign in to comment.