forked from fkling/astexplorer
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add markdown transformer - Add txt transformer
- Loading branch information
Showing
4 changed files
with
113 additions
and
30 deletions.
There are no files selected for viewing
10 changes: 7 additions & 3 deletions
10
src/parsers/md/transformers/textlint-markdown-to-ast/codeExample.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export default function (context) { | ||
console.log(context); | ||
return {} | ||
} | ||
const {Syntax, RuleError, report, getSource} = context; | ||
return { | ||
[Syntax.Str](node){ | ||
report(node, new Error("error")); | ||
} | ||
}; | ||
} |
73 changes: 46 additions & 27 deletions
73
src/parsers/md/transformers/textlint-markdown-to-ast/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,52 @@ | ||
import compileModule from '../../../utils/compileModule'; | ||
import pkg from 'textlint/package.json'; | ||
|
||
const ID = 'textlint'; | ||
const ID = 'textlint:markdown'; | ||
|
||
function formatResults(results, code) { | ||
const format = (message) => { | ||
return formatResult(message, code); | ||
}; | ||
return results.messages.length === 0 | ||
? 'Lint rule not fired.' | ||
: results.messages.map(format).join('').trim(); | ||
} | ||
|
||
function formatResult(result, code) { | ||
const pointer = '-'.repeat(result.column - 1) + '^'; | ||
console.log(code.split('\n')[result.line - 1]); | ||
return ` | ||
| ${result.message} (at ${result.line}:${result.column}) | ||
${code.split('\n')[result.line - 1] || ''} | ||
| ${pointer} | ||
`; | ||
} | ||
|
||
export default { | ||
id: ID, | ||
displayName: ID, | ||
version: pkg.version, | ||
homepage: pkg.homepage, | ||
|
||
defaultParserID: 'textlint:markdown-to-ast', | ||
|
||
loadTransformer(callback) { | ||
require(['textlint/lib/textlint-core', 'babel-core'], (TextLintCore, babel) => { | ||
callback({ TextLintCore, babel }); | ||
}) | ||
}, | ||
|
||
transform({TextLintCore, babel}, transformCode, code) { | ||
console.log(TextLintCore); | ||
const textlintCore = new TextLintCore(); | ||
let rule = compileModule( // eslint-disable-line no-shadow | ||
babel.transform(transformCode).code | ||
); | ||
textlintCore.setupRules({ | ||
'astExplorerRule': rule | ||
}); | ||
return textlintCore.lintText(code, ".md").then(result => { | ||
return JSON.stringify(result); | ||
}); | ||
} | ||
id: ID, | ||
displayName: ID, | ||
version: pkg.version, | ||
homepage: pkg.homepage, | ||
|
||
defaultParserID: 'textlint:markdown-to-ast', | ||
|
||
loadTransformer(callback) { | ||
require(['textlint/lib/textlint-core', 'babel-core'], (TextLintCore, babel) => { | ||
callback({TextLintCore, babel}); | ||
}) | ||
}, | ||
|
||
transform({TextLintCore, babel}, transformCode, code) { | ||
const textlintCore = new TextLintCore(); | ||
let rule = compileModule( // eslint-disable-line no-shadow | ||
babel.transform(transformCode).code | ||
); | ||
textlintCore.setupRules({ | ||
'astExplorerRule': rule, | ||
}); | ||
return textlintCore.lintText(code, '.md').then(result => { | ||
return formatResults(result, code); | ||
}); | ||
}, | ||
}; | ||
|
8 changes: 8 additions & 0 deletions
8
src/parsers/txt/transformers/textlint-txt-to-ast/codeExample.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function (context) { | ||
const {Syntax, RuleError, report, getSource} = context; | ||
return { | ||
[Syntax.Str](node){ | ||
report(node, new Error("error")); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import compileModule from '../../../utils/compileModule'; | ||
import pkg from '../../../../../node_modules/textlint/package.json'; | ||
|
||
const ID = 'textlint:txt'; | ||
|
||
function formatResults(results, code) { | ||
const format = (message) => { | ||
return formatResult(message, code); | ||
}; | ||
return results.messages.length === 0 | ||
? 'Lint rule not fired.' | ||
: results.messages.map(format).join('').trim(); | ||
} | ||
|
||
function formatResult(result, code) { | ||
const pointer = '-'.repeat(result.column - 1) + '^'; | ||
console.log(code.split('\n')[result.line - 1]); | ||
return ` | ||
| ${result.message} (at ${result.line}:${result.column}) | ||
${code.split('\n')[result.line - 1] || ''} | ||
| ${pointer} | ||
`; | ||
} | ||
|
||
export default { | ||
id: ID, | ||
displayName: ID, | ||
version: pkg.version, | ||
homepage: pkg.homepage, | ||
|
||
defaultParserID: 'textlint:txt-to-ast', | ||
|
||
loadTransformer(callback) { | ||
require(['../../../../../node_modules/textlint/lib/textlint-core', 'babel-core'], (TextLintCore, babel) => { | ||
callback({TextLintCore, babel}); | ||
}) | ||
}, | ||
|
||
transform({TextLintCore, babel}, transformCode, code) { | ||
const textlintCore = new TextLintCore(); | ||
let rule = compileModule( // eslint-disable-line no-shadow | ||
babel.transform(transformCode).code | ||
); | ||
textlintCore.setupRules({ | ||
'astExplorerRule': rule, | ||
}); | ||
return textlintCore.lintText(code, '.txt').then(result => { | ||
return formatResults(result, code); | ||
}); | ||
}, | ||
}; | ||
|