doc/markdownlint-rules-grav-pages/rules/valid-metadata-block.js
OniriCorpe 7a01793062
Improve markdown formatting of pages, a lot of them are improperly formatted (#2429)
* Revert "Revert "markdown format""

* fix formating

* fix readme

* add .markdownlint.json

* add markdownlint-rules-grav-pages

* add markdownlint-rules-grav-pages files

* add license for Markdown Lint Rules for Grav Pages

* fix [figure] mess

* fix [figure] mess 2

* fix [figure] mess 3

* maj .gitignore

* various fixes

* fix markdownlint-rules-grav-pages

* second formater pass

* various manual fixes

* add .markdownlintignore

* markdownlintignore: auto-generated pages

* disable proper-names for html_elements

* another bunch of various markdown fixes

* Update pages/02.administer/10.install/20.dns_config/dns_config.es.md

Co-authored-by: tituspijean <titus+yunohost@pijean.ovh>

---------

Co-authored-by: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com>
Co-authored-by: tituspijean <titus+yunohost@pijean.ovh>
2024-03-23 08:59:52 +01:00

69 lines
2.2 KiB
JavaScript

const YAML = require('yamljs');
const { Validator } = require('jsonschema');
// See https://learn.getgrav.org/content/headers
const metadataSchema = require('./frontmatter.schema.json');
module.exports = {
names: ['valid-metadata-block'],
description: 'Rule that reports if a file does not have a valid grav metadata block',
tags: ['test'],
function: function rule(params, onError) {
if (!params.frontMatterLines || params.frontMatterLines.length < 3) {
onError({
lineNumber: 1,
detail: 'Missing grav metadata block',
});
return;
}
const frontMatterLines = params.frontMatterLines.filter((line) => !!line);
if (frontMatterLines[0] !== '---') {
onError({
lineNumber: 1,
detail: "Grav metadata block has to start with a '---'",
context: frontMatterLines[0],
});
return;
}
if (frontMatterLines[frontMatterLines.length - 1] !== '---') {
onError({
lineNumber: 1,
detail: "Grav metadata block has to end with a '---'",
context: frontMatterLines[frontMatterLines.length - 1],
});
return;
}
const yamlString = frontMatterLines.slice(1, -1).join('\n');
let yamlDocument;
try {
yamlDocument = YAML.parse(yamlString);
} catch (err) {
onError({
lineNumber: 1,
detail: 'Error parsing grav metadata block',
context: err.toString(),
});
return;
}
if (!yamlDocument) {
onError({
lineNumber: 1,
detail: 'Grav metadata is not a valid yaml document',
context: yamlString,
});
return;
}
const v = new Validator();
const validation = v.validate(yamlDocument, metadataSchema);
validation.errors.forEach((validationError) => {
onError({
lineNumber: 1,
detail: validationError.toString(),
context: yamlString,
});
});
},
};