From 5f5437723ef414ccf9bf9fc6b78677561cad1f92 Mon Sep 17 00:00:00 2001 From: azu Date: Sun, 27 Mar 2016 13:36:13 +0900 Subject: [PATCH] Add textlint's transformers - Add markdown transformer - Add txt transformer --- .../textlint-markdown-to-ast/codeExample.txt | 10 ++- .../textlint-markdown-to-ast/index.js | 73 ++++++++++++------- .../textlint-txt-to-ast/codeExample.txt | 8 ++ .../transformers/textlint-txt-to-ast/index.js | 52 +++++++++++++ 4 files changed, 113 insertions(+), 30 deletions(-) create mode 100644 src/parsers/txt/transformers/textlint-txt-to-ast/codeExample.txt create mode 100644 src/parsers/txt/transformers/textlint-txt-to-ast/index.js diff --git a/src/parsers/md/transformers/textlint-markdown-to-ast/codeExample.txt b/src/parsers/md/transformers/textlint-markdown-to-ast/codeExample.txt index 32b5a7e0..0bd4d4d8 100644 --- a/src/parsers/md/transformers/textlint-markdown-to-ast/codeExample.txt +++ b/src/parsers/md/transformers/textlint-markdown-to-ast/codeExample.txt @@ -1,4 +1,8 @@ export default function (context) { - console.log(context); - return {} -} \ No newline at end of file + const {Syntax, RuleError, report, getSource} = context; + return { + [Syntax.Str](node){ + report(node, new Error("error")); + } + }; +} diff --git a/src/parsers/md/transformers/textlint-markdown-to-ast/index.js b/src/parsers/md/transformers/textlint-markdown-to-ast/index.js index 801eb787..85ec8638 100644 --- a/src/parsers/md/transformers/textlint-markdown-to-ast/index.js +++ b/src/parsers/md/transformers/textlint-markdown-to-ast/index.js @@ -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); + }); + }, }; + diff --git a/src/parsers/txt/transformers/textlint-txt-to-ast/codeExample.txt b/src/parsers/txt/transformers/textlint-txt-to-ast/codeExample.txt new file mode 100644 index 00000000..0bd4d4d8 --- /dev/null +++ b/src/parsers/txt/transformers/textlint-txt-to-ast/codeExample.txt @@ -0,0 +1,8 @@ +export default function (context) { + const {Syntax, RuleError, report, getSource} = context; + return { + [Syntax.Str](node){ + report(node, new Error("error")); + } + }; +} diff --git a/src/parsers/txt/transformers/textlint-txt-to-ast/index.js b/src/parsers/txt/transformers/textlint-txt-to-ast/index.js new file mode 100644 index 00000000..9d651c4a --- /dev/null +++ b/src/parsers/txt/transformers/textlint-txt-to-ast/index.js @@ -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); + }); + }, +}; +