|
| 1 | +const fs = require("fs") |
| 2 | +const path = require("path") |
| 3 | +const matter = require("gray-matter") |
| 4 | +const argv = require("minimist")(process.argv.slice(2)) |
| 5 | + |
| 6 | +const LANG_ARG = argv.lang || null |
| 7 | +const PATH_TO_INTL_MARKDOWN = "./src/content/translations/" |
| 8 | +const PATH_TO_ALL_CONTENT = "./src/content/" |
| 9 | +const TUTORIAL_DATE_REGEX = new RegExp("\\d{4}-\\d{2}-\\d{2}") |
| 10 | +const WHITE_SPACE_IN_LINK_TEXT = new RegExp( |
| 11 | + "\\[\\s.+\\]\\( | \\[.+\\s\\]\\(", |
| 12 | + "g" |
| 13 | +) |
| 14 | +const BROKEN_LINK_REGEX = new RegExp( |
| 15 | + "\\[[^\\]]+\\]\\([^\\)\\s]+\\s[^\\)]+\\)", |
| 16 | + "g" |
| 17 | +) |
| 18 | +const HTML_TAGS = ["</code", "</p>"] |
| 19 | +const SPELLING_MISTAKES = [ |
| 20 | + "Ethreum", |
| 21 | + "Etherum", |
| 22 | + "Etherium", |
| 23 | + "Etheruem", |
| 24 | + "Etereum", |
| 25 | + "Eterium", |
| 26 | + "Etherem", |
| 27 | + "Etheerum", |
| 28 | + "Ehtereum", |
| 29 | + "Eferum", |
| 30 | +] |
| 31 | +const CASE_SENSITVE_SPELLING_MISTAKES = ["Thereum", "Metamask", "Github"] |
| 32 | +// Ideas: |
| 33 | +// Regex for explicit lang path (e.g. /en/) && for glossary links (trailing slash breaks links e.g. /glossary/#pos/ doesn't work) |
| 34 | +// We should have case sensitive spelling mistakes && check they are not in links. |
| 35 | + |
| 36 | +const langsArray = fs.readdirSync(PATH_TO_INTL_MARKDOWN) |
| 37 | +langsArray.push("en") |
| 38 | + |
| 39 | +function getAllMarkdownPaths(dirPath, arrayOfMarkdownPaths = []) { |
| 40 | + let files = fs.readdirSync(dirPath) |
| 41 | + |
| 42 | + arrayOfMarkdownPaths = arrayOfMarkdownPaths || [] |
| 43 | + |
| 44 | + for (const file of files) { |
| 45 | + if (fs.statSync(dirPath + "/" + file).isDirectory()) { |
| 46 | + arrayOfMarkdownPaths = getAllMarkdownPaths( |
| 47 | + dirPath + "/" + file, |
| 48 | + arrayOfMarkdownPaths |
| 49 | + ) |
| 50 | + } else { |
| 51 | + const filePath = path.join(dirPath, "/", file) |
| 52 | + |
| 53 | + if (filePath.includes(".md")) { |
| 54 | + arrayOfMarkdownPaths.push(filePath) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return arrayOfMarkdownPaths |
| 60 | +} |
| 61 | + |
| 62 | +function sortMarkdownPathsIntoLanguages(files) { |
| 63 | + const languages = langsArray.reduce((accumulator, value) => { |
| 64 | + return { ...accumulator, [value]: [] } |
| 65 | + }, {}) |
| 66 | + |
| 67 | + for (const file of files) { |
| 68 | + const isTranslation = file.includes("/translations/") |
| 69 | + const langIndex = file.indexOf("/translations/") + 14 |
| 70 | + const isFourCharLang = file.includes("pt-br") || file.includes("zh-tw") |
| 71 | + const charactersToSlice = isFourCharLang ? 5 : 2 |
| 72 | + |
| 73 | + const lang = isTranslation |
| 74 | + ? file.slice(langIndex, langIndex + charactersToSlice) |
| 75 | + : "en" |
| 76 | + |
| 77 | + if (LANG_ARG) { |
| 78 | + if (LANG_ARG === lang) { |
| 79 | + languages[lang].push(file) |
| 80 | + } |
| 81 | + } else { |
| 82 | + languages[lang].push(file) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return languages |
| 87 | +} |
| 88 | + |
| 89 | +function processFrontmatter(path, lang) { |
| 90 | + const file = fs.readFileSync(path, "utf-8") |
| 91 | + const frontmatter = matter(file).data |
| 92 | + |
| 93 | + if (!frontmatter.title) { |
| 94 | + console.warn(`Missing 'title' frontmatter at ${path}:`) |
| 95 | + } |
| 96 | + // Description commented out as there are a lot of them missing :-)! |
| 97 | + // if (!frontmatter.description) { |
| 98 | + // console.warn(`Missing 'description' frontmatter at ${path}:`) |
| 99 | + // } |
| 100 | + if (!frontmatter.lang) { |
| 101 | + console.error(`Missing 'lang' frontmatter at ${path}: Expected: ${lang}:'`) |
| 102 | + } else if (!(frontmatter.lang === lang)) { |
| 103 | + console.error( |
| 104 | + `Invalid 'lang' frontmatter at ${path}: Expected: ${lang}'. Received: ${frontmatter.lang}.` |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + if (path.includes("/tutorials/")) { |
| 109 | + if (!frontmatter.published) { |
| 110 | + console.warn(`Missing 'published' frontmatter at ${path}:`) |
| 111 | + } else { |
| 112 | + try { |
| 113 | + let stringDate = frontmatter.published.toISOString().slice(0, 10) |
| 114 | + const dateIsFormattedCorrectly = TUTORIAL_DATE_REGEX.test(stringDate) |
| 115 | + |
| 116 | + if (!dateIsFormattedCorrectly) { |
| 117 | + console.warn( |
| 118 | + `Invalid 'published' frontmatter at ${path}: Expected: 'YYYY-MM-DD' Received: ${frontmatter.published}` |
| 119 | + ) |
| 120 | + } |
| 121 | + } catch (e) { |
| 122 | + console.warn( |
| 123 | + `Invalid 'published' frontmatter at ${path}: Expected: 'YYYY-MM-DD' Received: ${frontmatter.published}` |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +function processMarkdown(path) { |
| 131 | + const markdownFile = fs.readFileSync(path, "utf-8") |
| 132 | + let brokenLinkMatch |
| 133 | + |
| 134 | + while ((brokenLinkMatch = BROKEN_LINK_REGEX.exec(markdownFile))) { |
| 135 | + const lineNumber = getLineNumber(markdownFile, brokenLinkMatch.index) |
| 136 | + console.warn(`Broken link found: ${path}:${lineNumber}`) |
| 137 | + |
| 138 | + // if (!BROKEN_LINK_REGEX.global) break |
| 139 | + } |
| 140 | + |
| 141 | + // TODO: refactor history pages to use a component for network upgrade summaries |
| 142 | + // TODO: create .env commit warning component for tutorials |
| 143 | + // Ignore tutorials with Javascript and ExpandableCards |
| 144 | + /* Commented this out due to console noise (but they are things we should fix!) |
| 145 | + if (!(path.includes("/history/")) && !(markdownFile.includes("```javascript")) && !(markdownFile.includes("ExpandableCard"))) { |
| 146 | + for (const tag of HTML_TAGS) { |
| 147 | + |
| 148 | + const htmlTagRegex = new RegExp(tag, "g") |
| 149 | + let htmlTagMatch |
| 150 | +
|
| 151 | + while ((htmlTagMatch = htmlTagRegex.exec(markdownFile))) { |
| 152 | + const lineNumber = getLineNumber(markdownFile, htmlTagMatch.index) |
| 153 | + console.warn(`Warning: ${tag} tag in markdown at ${path}:${lineNumber}`) |
| 154 | + |
| 155 | + if (!htmlTagRegex.global) break |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + */ |
| 160 | + |
| 161 | + // Commented out as 296 instances of whitespace in link texts |
| 162 | + // let whiteSpaceInLinkTextMatch |
| 163 | + |
| 164 | + // while ((whiteSpaceInLinkTextMatch = WHITE_SPACE_IN_LINK_TEXT.exec(markdownFile))) { |
| 165 | + // const lineNumber = getLineNumber(markdownFile, whiteSpaceInLinkTextMatch.index) |
| 166 | + // console.warn(`White space in link found: ${path}:${lineNumber}`) |
| 167 | + // } |
| 168 | + |
| 169 | + checkMarkdownSpellingMistakes(path, markdownFile, SPELLING_MISTAKES) |
| 170 | + // Turned this off for testing as there are lots of Github (instead of GitHub) and Metamask (instead of MetaMask). |
| 171 | + // checkMarkdownSpellingMistakes(path, markdownFile, CASE_SENSITVE_SPELLING_MISTAKES, true) |
| 172 | +} |
| 173 | + |
| 174 | +function checkMarkdownSpellingMistakes( |
| 175 | + path, |
| 176 | + file, |
| 177 | + spellingMistakes, |
| 178 | + caseSensitive = false |
| 179 | +) { |
| 180 | + for (const mistake of spellingMistakes) { |
| 181 | + const mistakeRegex = caseSensitive |
| 182 | + ? new RegExp(mistake, "g") |
| 183 | + : new RegExp(mistake, "gi") |
| 184 | + let spellingMistakeMatch |
| 185 | + |
| 186 | + while ((spellingMistakeMatch = mistakeRegex.exec(file))) { |
| 187 | + const lineNumber = getLineNumber(file, spellingMistakeMatch.index) |
| 188 | + console.warn( |
| 189 | + `Spelling mistake "${mistake}" found at ${path}:${lineNumber}` |
| 190 | + ) |
| 191 | + } |
| 192 | + |
| 193 | + if (!mistakeRegex.global) break |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +function getLineNumber(file, index) { |
| 198 | + const fileSubstring = file.substring(0, index) |
| 199 | + const lines = fileSubstring.split("\n") |
| 200 | + const linePosition = lines.length |
| 201 | + const charPosition = lines[lines.length - 1].length + 1 |
| 202 | + const lineNumber = `${linePosition}:${charPosition}` |
| 203 | + |
| 204 | + return lineNumber |
| 205 | +} |
| 206 | + |
| 207 | +function checkMarkdown() { |
| 208 | + const markdownPaths = getAllMarkdownPaths(PATH_TO_ALL_CONTENT) |
| 209 | + const markdownPathsByLang = sortMarkdownPathsIntoLanguages(markdownPaths) |
| 210 | + |
| 211 | + for (const lang in markdownPathsByLang) { |
| 212 | + for (const path of markdownPathsByLang[lang]) { |
| 213 | + processFrontmatter(path, lang) |
| 214 | + processMarkdown(path) |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +checkMarkdown() |
0 commit comments