|
| 1 | +// LICENSE : MIT |
| 2 | +"use strict"; |
| 3 | +import {getKeywords} from "stemming-x-keywords"; |
| 4 | +import path from "path"; |
| 5 | +import fs from "fs"; |
| 6 | +import mdast from "mdast"; |
| 7 | +import parents from "unist-util-parents"; |
| 8 | +import select from "unist-util-select"; |
| 9 | +import isUnist from "unist-util-is"; |
| 10 | +import nlcstToString from "nlcst-to-string"; |
| 11 | + |
| 12 | +const rootDir = path.join(__dirname, ".."); |
| 13 | +const OrganizationText = fs.readFileSync(path.join(rootDir, "ORGANIZATION.md"), "utf-8"); |
| 14 | +function isNotContain(content, keywords) { |
| 15 | + // 含んでないものだけを返す |
| 16 | + return keywords.filter(keyword => { |
| 17 | + return content.indexOf(keyword) === -1; |
| 18 | + }); |
| 19 | +} |
| 20 | +function findAllAfter(ast, node, type) { |
| 21 | + let results = []; |
| 22 | + let children = ast.children; |
| 23 | + let index = 0, length = children.length; |
| 24 | + let isFound = false; |
| 25 | + while (++index < length) { |
| 26 | + let child = children[index]; |
| 27 | + if (isUnist(node, child)) { |
| 28 | + isFound = true; |
| 29 | + } else if (isFound) { |
| 30 | + if (isUnist(type, child)) { |
| 31 | + results.push(child); |
| 32 | + } else { |
| 33 | + break; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return results; |
| 39 | +} |
| 40 | + |
| 41 | +function isAlreadyCheckKeyword(list, keyword) { |
| 42 | + return list.indexOf(keyword) !== -1; |
| 43 | +} |
| 44 | + |
| 45 | +// P ASTからキーワードを抽出する |
| 46 | +function getKeywordsOfParagraphsAsync(paragraphs) { |
| 47 | + let _keywords = []; |
| 48 | + let promiseList = paragraphs.map(p => { |
| 49 | + let text = nlcstToString(p); |
| 50 | + return getKeywords(text).then(keywords => { |
| 51 | + _keywords = _keywords.concat(keywords); |
| 52 | + }); |
| 53 | + }); |
| 54 | + return Promise.all(promiseList).then(()=> { |
| 55 | + return _keywords; |
| 56 | + }); |
| 57 | +} |
| 58 | +function checkKeyword(text) { |
| 59 | + let ast = mdast.parse(text); |
| 60 | + let headerLinks = select(parents(ast), "heading link[href]"); |
| 61 | + let paragraphList = headerLinks.map(link => { |
| 62 | + let filePath = path.resolve(rootDir, link.href); |
| 63 | + let paragraphs = findAllAfter(ast, link.parent.node, "paragraph"); |
| 64 | + return getKeywordsOfParagraphsAsync(paragraphs).then(keywords => { |
| 65 | + return { |
| 66 | + filePath: filePath, |
| 67 | + content: fs.readFileSync(filePath, "utf-8"), |
| 68 | + keywords: keywords |
| 69 | + }; |
| 70 | + }); |
| 71 | + }); |
| 72 | + return Promise.all(paragraphList).then(results => { |
| 73 | + let confirmedKeywords = []; |
| 74 | + return results.forEach(({filePath, content, keywords}) => { |
| 75 | + let unusedKeywords = isNotContain(content, keywords); |
| 76 | + let isChecked = isAlreadyCheckKeyword.bind(null, confirmedKeywords); |
| 77 | + if (unusedKeywords.length === 0) { |
| 78 | + // 使用済みのキーワードを登録 |
| 79 | + confirmedKeywords = confirmedKeywords.concat(keywords); |
| 80 | + return; |
| 81 | + } |
| 82 | + if (unusedKeywords.every(isChecked)) { |
| 83 | + console.log(unusedKeywords.join(",") + "はチェック済み"); |
| 84 | + return; |
| 85 | + } |
| 86 | + throw new Error(`"${unusedKeywords.join(",")}" are not used in ${filePath}`); |
| 87 | + }); |
| 88 | + }); |
| 89 | +} |
| 90 | +// キーワードが書くコンテンツに含まれているかをテストする |
| 91 | +describe("keywords", function () { |
| 92 | + it("Each chapter contain the keyword", function () { |
| 93 | + return checkKeyword(OrganizationText); |
| 94 | + }); |
| 95 | +}); |
0 commit comments