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