1
1
// LICENSE : MIT
2
2
"use strict" ;
3
3
import { getFilePathListAsync } from "gitbook-summary-to-path" ;
4
+ let getKeywords = require ( "stemming-x-keywords" ) . getKeywords ;
4
5
import path from "path" ;
5
6
import fs from "fs" ;
6
7
import mdast from "mdast" ;
7
- var select = require ( 'unist-util-select' ) ;
8
+ import parents from "unist-util-parents" ;
9
+ import select from "unist-util-select" ;
10
+ import isUnist from "unist-util-is" ;
11
+ import nlcstToString from "nlcst-to-string" ;
12
+
13
+
8
14
const rootDir = path . join ( __dirname , ".." ) ;
9
15
const keywordInfo = require ( "./keywords.json" ) ;
10
- const Org = fs . readFileSync ( __dirname + "/../ORGANIZATION.md" , "utf-8" ) ;
11
- function getKeywords ( matchPath ) {
16
+ const Org = fs . readFileSync ( __dirname + "/../ORGANIZATION.md" , "utf-8" ) ;
17
+ function getSummary ( ) {
18
+ if ( getSummary . cache ) {
19
+ return Promise . resolve ( getSummary . cache ) ;
20
+ }
12
21
return getFilePathListAsync ( __dirname + "/../SUMMARY.md" ) . then ( fileList => {
13
- let filteredList = fileList . filter ( filePath => {
14
- return filePath . indexOf ( matchPath ) !== - 1 ;
15
- } ) ;
16
- return filteredList [ 0 ] ;
17
- } ) . then ( filePath => {
18
- let relativePath = path . relative ( rootDir , filePath ) ;
19
- let content = fs . readFileSync ( filePath , "utf-8" ) ;
20
- return {
21
- filePath,
22
- content,
23
- keywords : keywordInfo [ relativePath ]
24
- } ;
22
+ getSummary . cache = fileList ;
23
+ return fileList ;
25
24
} ) ;
26
25
}
27
26
@@ -31,23 +30,61 @@ function isNotContain(content, keywords) {
31
30
return content . indexOf ( keyword ) === - 1 ;
32
31
} ) ;
33
32
}
33
+ function findAllAfter ( ast , node , type ) {
34
+ let results = [ ] ;
35
+ let children = ast . children ;
36
+ let index = 0 , length = children . length ;
37
+ let isFound = false ;
38
+ while ( ++ index < length ) {
39
+ let child = children [ index ] ;
40
+ if ( isUnist ( node , child ) ) {
41
+ isFound = true ;
42
+ } else if ( isFound ) {
43
+ if ( isUnist ( type , child ) ) {
44
+ results . push ( child ) ;
45
+ } else {
46
+ break ;
47
+ }
48
+ }
49
+ }
50
+
51
+ return results ;
52
+ }
34
53
// キーワードが書くコンテンツに含まれているかをテストする
35
54
describe ( "keywords" , function ( ) {
36
- it ( "Each chapter contain the keyword" , function ( done ) {
55
+ it ( "Each chapter contain the keyword" , function ( ) {
37
56
let ast = mdast . parse ( Org ) ;
38
- let headingPrs = select ( ast , "heading, heading + paragraph" ) ;
39
- let promises = Object . keys ( keywordInfo ) . map ( key => {
40
- return getKeywords ( key ) . then ( ( { filePath, content, keywords} ) => {
57
+ let headerLinks = select ( parents ( ast ) , "heading link[href]" ) ;
58
+ let filePathList = headerLinks . map ( link => {
59
+ return path . resolve ( rootDir , link . href ) ;
60
+ } ) ;
61
+ let paragraphList = headerLinks . map ( link => {
62
+ let filePath = path . resolve ( rootDir , link . href ) ;
63
+ let paragraphs = findAllAfter ( ast , link . parent . node , "paragraph" ) ;
64
+ let results = {
65
+ filePath : filePath ,
66
+ content : fs . readFileSync ( filePath , "utf-8" ) ,
67
+ keywords : [ ]
68
+ } ;
69
+ let keywords = paragraphs . map ( p => {
70
+ let text = nlcstToString ( p ) ;
71
+ return getKeywords ( text ) . then ( a => {
72
+ results . keywords = results . keywords . concat ( a )
73
+ } ) ;
74
+ } ) ;
75
+ return Promise . all ( keywords ) . then ( ( ) => {
76
+ return results ;
77
+ } ) ;
78
+ } ) ;
79
+ let promises = Promise . all ( paragraphList ) . then ( results => {
80
+ return results . forEach ( ( { filePath, content, keywords} ) => {
41
81
let unusedKeywords = isNotContain ( content , keywords ) ;
42
82
if ( unusedKeywords . length === 0 ) {
43
- return Promise . resolve ( ) ;
44
- } else {
45
- return Promise . reject ( new Error ( `"${ unusedKeywords . join ( "," ) } " are not used in ${ filePath } ` ) ) ;
83
+ return ;
46
84
}
85
+ throw new Error ( `"${ unusedKeywords . join ( "," ) } " are not used in ${ filePath } ` ) ;
47
86
} ) ;
48
87
} ) ;
49
- Promise . all ( promises ) . then ( ( ) => {
50
- done ( ) ;
51
- } , done ) ;
88
+ return promises ;
52
89
} ) ;
53
90
} ) ;
0 commit comments