Something went wrong. Try again.
Join the GitHub Graduation Yearbook and "walk the stage" on June 5.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091const actionEvent = require('./action-event.js');const path = require('path')const yaml = require('yaml')const md = require('markdown-it')
const pullAuthor = actionEvent.pull.user.loginconst expectedPath = `_data/${pullAuthor}`const characterLimits = { name: 28, institution: 58, quote: 100 }
class FileVaidator { constructor() {
}
async isMarkdownValid(markdown) { // hack to import a module TOO Enable modules and swap all the requires const metadataBlock = (await import("markdown-it-metadata-block")).default; const meta = {} const errors = [] let mdContent = false
const mdParser = md({ html: true, linkify: true, typographer: true }).use(metadataBlock,{ parseMetadata: yaml.parse, meta: meta });
try { mdContent = mdParser.render(markdown) } catch(err) { console.log("markdown error: " + err) errors.push(`*The markdown content in \`${expectedPath}/${pullAuthor}.md\` has syntax errors*`) }
if(mdContent !== false) { if(meta.github_user !== pullAuthor) { errors.push(`*The yaml content in \`${expectedPath}/${pullAuthor}.md\` must contain your github username*`) }
for(const key of [ "name", "institution", "quote" ]) { if(!meta[key]) { errors.push(`*The attribute \`${key}\` is required in \`${expectedPath}/${pullAuthor}.md\`*`) } else if(meta[key].length > characterLimits[key]) { errors.push(`*The value for \`${key}\` can only have **${characterLimits[key]}** characters max (I see **${meta[key].length }**)*`) } } } else { errors.push(`*\`${expectedPath}/${pullAuthor}.md\` does not contain any yaml metadata*`) }
return { isValid: !errors.length, errors: errors } }
isValidPaths(filePaths=[]) { const errors = [] const invalidPaths = [] let invalidDirectory = false let InvalidMarkdownFile = true let isValid = true let pathData
for(let filePath of filePaths) { pathData = path.parse(filePath)
if(pathData.dir !== expectedPath) { invalidPaths.push("`" + filePath + "`") invalidDirectory = true }
if(pathData.base === `${pullAuthor}.md`) { InvalidMarkdownFile = false } }
if(InvalidMarkdownFile) { errors.push(`*The required markdown file does not exist, please ensure the file \`${expectedPath}/${pullAuthor}.md\` exists*`) }
if(invalidDirectory) { errors.push(`*Please ensure all changes are contained within the \`${expectedPath}/\` directory. Invalid file paths:* \n\n\t* ${invalidPaths.join('\n\t* ')}\n`) }
return { isValid: !errors.length, errors } }}
module.exports = new FileVaidator()