Skip to content

Commit

Permalink
feat: add releaseDate check to verify-mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
tabathadelane committed Jan 23, 2024
1 parent c851b5d commit 4097bc6
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
1 change: 1 addition & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Otherwise, it will read all files under `/src/content/` and `src/i18n/content`.
- markdown and JSX syntax
- valid yaml frontmatter
- We also check for the required field `freshnessValidatedDate` which must be a date (`YYYY-MM-DD`) or `never`
- `release-notes`, `security-bulletins` and `whats-new` posts will also be scanned for a required `releaseDate` field in the frontmatter.
- `<img />` sources and imports which utilizes the [image-import-utils script](#image-import-utils.js)
- this util has its own progress bar and error output section in the terminal

Expand Down
78 changes: 77 additions & 1 deletion scripts/utils/__tests__/frontmatter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { frontmatter, validateFreshnessDate } = require('../frontmatter');
const {
frontmatter,
validateFreshnessDate,
validateReleaseDate,
} = require('../frontmatter');

const mdxString = `---
howdy: cowboy 🤠
Expand Down Expand Up @@ -130,3 +134,75 @@ describe('freshness frontmatter field', () => {
expect(error.message).toEqual(expectedError);
});
});

const happyReleaseDateMdxString = `---
howdy: cowboy 🤠
list:
- item 1
- item 2
releaseDate: '2021-12-02'
---
some content!
`;

const invalidReleaseDateMdxString = `---
howdy: cowboy 🤠
list:
- item 1
- item 2
releaseDate: '23-12-02'
---
some content!
`;
const badFormatReleaseDateMdxString = `---
howdy: cowboy 🤠
list:
- item 1
- item 2
releaseDate: 2023
---
some content!
`;

const missingReleaseDateMdxString = `---
howdy: cowboy 🤠
list:
- item 1
- item 2
---
some content!
`;

describe('releaseDate frontmatter field', () => {
it('should pass the check with valid "never" value', () => {
const error = validateReleaseDate(happyReleaseDateMdxString);

expect(error).toBeFalsy;
});

it('should throw error for invalid date value', () => {
const error = validateReleaseDate(invalidReleaseDateMdxString);

const expectedError =
"releaseDate is not a valid value. Must be date string formatted like 'YYYY-MM-DD' wrapped in single quotes";

expect(error.message).toEqual(expectedError);
});

it('should throw error for valid date but wrong format', () => {
const error = validateReleaseDate(badFormatReleaseDateMdxString);

const expectedError =
"releaseDate is not a valid value. Must be date string formatted like 'YYYY-MM-DD' wrapped in single quotes";

expect(error.message).toEqual(expectedError);
});

it('should throw error missing releaseDate field', () => {
const error = validateReleaseDate(missingReleaseDateMdxString);

const expectedError = 'releaseDate field missing from frontmatter';

expect(error.message).toEqual(expectedError);
});
});
64 changes: 64 additions & 0 deletions scripts/utils/frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,71 @@ const validateFreshnessDate = (mdString) => {
return error;
};

/**
* Parse the frontmatter and check for required releaseDate field on whats-new, security-bulletins, and release-notes.
* Valid format is `YYYY-MM-DD`
*
* @example
* ```yaml
* ---
* title: This is a security bulletin
* releaseDate: '2021-03-15'
* ---
* ```
*
*```js
* // error.message = undefined
* ```
*
* ```yaml
* ---
* title: This is a security bulletin with an invalid date
* releaseDate: 21-03-15
* ---
* ```
*
*```js
* // error.message = releaseDate is not a valid value. Must be date string formatted like 'YYYY-MM-DD' wrapped in single quotes
* ```
*
*
*
* @param {string} mdString - Full content of the MD(X) file to parse.
* @returns error | undefined
*/

const validateReleaseDate = (mdString) => {
let error;

const { data } = grayMatter(mdString, {
engines: {
yaml: {
parse: (string) => yaml.safeLoad(string, { schema: yaml.JSON_SCHEMA }),
},
},
});

const isValidDate = (date) => {
const regex = /\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/;
return regex.test(date);
};
// releaseDate is a required field and must be a date wrapped in single quotes
if (data.releaseDate) {
if (!isValidDate(data.releaseDate)) {
error = new Error(
"releaseDate is not a valid value. Must be date string formatted like 'YYYY-MM-DD' wrapped in single quotes"
);
}
} else {
error = new Error('releaseDate field missing from frontmatter');
}

return error;
};

module.exports = {
frontmatter,
validateFreshnessDate,
validateReleaseDate,
};
17 changes: 16 additions & 1 deletion scripts/verify_mdx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable no-console */
const { frontmatter, validateFreshnessDate } = require('./utils/frontmatter');
const {
frontmatter,
validateFreshnessDate,
validateReleaseDate,
} = require('./utils/frontmatter');
const { verifyImageImports } = require('./utils/image-import-utils.js');
const mdx = require('@mdx-js/mdx');
const fs = require('fs');
Expand Down Expand Up @@ -92,6 +96,10 @@ const readFile = async (filePath) => {
(excludedPath) => filePath.includes(excludedPath)
);

const includeInReleaseDateRegex = /src\/(?!i18n).*(\/security-bulletins\/|\/release-notes\/|\/whats-new\/).*(?<!index)(.mdx|.md)/;

const shouldValidateReleaseDate = includeInReleaseDateRegex.test(filePath);

const { error } = frontmatter(mdxText);

if (error != null) {
Expand All @@ -106,6 +114,13 @@ const readFile = async (filePath) => {
\x1b[31m${error.message}\x1b[0m`);
failed = true;
}
} else if (shouldValidateReleaseDate) {
const error = validateReleaseDate(mdxText);
if (error) {
mdxErrors.push(`\x1b[35m Frontmatter field error:\x1b[0m ${filePath} \n
\x1b[31m${error.message}\x1b[0m`);
failed = true;
}
}

return failed ? filePath : null;
Expand Down

0 comments on commit 4097bc6

Please sign in to comment.