@@ -4,8 +4,38 @@ import {RuleHelper} from "textlint-rule-helper";
44import StructuredSource from "structured-source" ;
55import prh from "prh" ;
66import path from "path" ;
7- export default function ( context , options ) {
8- if ( typeof options === "undefined" || typeof options . rulePaths === "undefined" ) {
7+ function createPrhEngine ( rulePaths , baseDir ) {
8+ if ( rulePaths . length === 0 ) {
9+ return null ;
10+ }
11+ const prhEngine = prh . fromYAMLFilePath ( path . resolve ( baseDir , rulePaths [ 0 ] ) ) ;
12+ rulePaths . slice ( 1 ) . forEach ( ruleFilePath => {
13+ const config = prh . fromYAMLFilePath ( path . resolve ( baseDir , ruleFilePath ) ) ;
14+ prhEngine . merge ( config ) ;
15+ } ) ;
16+ return prhEngine ;
17+ }
18+ function createPrhEngineFromContents ( yamlContents ) {
19+ if ( yamlContents . length === 0 ) {
20+ return null ;
21+ }
22+ const prhEngine = prh . fromYAML ( null , yamlContents [ 0 ] ) ;
23+ yamlContents . slice ( 1 ) . forEach ( content => {
24+ const config = prh . fromYAML ( null , content ) ;
25+ prhEngine . merge ( config ) ;
26+ } ) ;
27+ return prhEngine ;
28+ }
29+ function mergePrh ( ...engines ) {
30+ const engines_ = engines . filter ( engine => ! ! engine ) ;
31+ const mainEngine = engines_ [ 0 ] ;
32+ engines_ . slice ( 1 ) . forEach ( engine => {
33+ mainEngine . merge ( engine ) ;
34+ } ) ;
35+ return mainEngine ;
36+ }
37+ export default function ( context , options = { } ) {
38+ if ( typeof options . ruleContents === "undefined" && typeof options . rulePaths === "undefined" ) {
939 throw new Error ( `textlint-rule-prh require Rule Options.
1040Please set .textlinrc:
1141{
@@ -17,17 +47,18 @@ Please set .textlinrc:
1747}
1848` ) ;
1949 }
20- var configFilePath = context . config ? context . config . configFile : null ;
50+ const textlintRcFilePath = context . config ? context . config . configFile : null ;
2151 // .textlinrc directory
22- var textlintRCDir = configFilePath ? path . dirname ( configFilePath ) : process . cwd ( ) ;
23- let helper = new RuleHelper ( context ) ;
24- let { Syntax, getSource, report, RuleError} = context ;
25- var rulePaths = options . rulePaths . slice ( ) ;
26- var config = prh . fromYAMLFilePath ( path . resolve ( textlintRCDir , rulePaths [ 0 ] ) ) ;
27- rulePaths . slice ( 1 ) . forEach ( function ( rulePath ) {
28- var c = prh . fromYAMLFilePath ( path . resolve ( textlintRCDir , rulePath ) ) ;
29- config . merge ( c ) ;
30- } ) ;
52+ const textlintRCDir = textlintRcFilePath ? path . dirname ( textlintRcFilePath ) : process . cwd ( ) ;
53+ // create prh config
54+ const rulePaths = options . rulePaths || [ ] ;
55+ const ruleContents = options . ruleContents || [ ] ;
56+ // yaml file + yaml contents
57+ const prhEngineContent = createPrhEngineFromContents ( ruleContents ) ;
58+ const prhEngineFiles = createPrhEngine ( rulePaths , textlintRCDir ) ;
59+ const prhEngine = mergePrh ( prhEngineFiles , prhEngineContent ) ;
60+ const helper = new RuleHelper ( context ) ;
61+ const { Syntax, getSource, report, RuleError} = context ;
3162 return {
3263 [ Syntax . Str ] ( node ) {
3364 if ( helper . isChildNode ( node , [ Syntax . Link , Syntax . Image , Syntax . BlockQuote , Syntax . Emphasis ] ) ) {
@@ -36,7 +67,7 @@ Please set .textlinrc:
3667 let text = getSource ( node ) ;
3768 // to get position from index
3869 let src = new StructuredSource ( text ) ;
39- let makeChangeSet = config . makeChangeSet ( null , text ) ;
70+ let makeChangeSet = prhEngine . makeChangeSet ( null , text ) ;
4071 makeChangeSet . diffs . forEach ( function ( changeSet ) {
4172 // | ----[match]------
4273 var slicedText = text . slice ( changeSet . index ) ;
0 commit comments