|
| 1 | +// Generated webworker code by textlint-script-compiler |
| 2 | +import { TextlintKernel } from "@textlint/kernel"; |
| 3 | +import { moduleInterop } from "@textlint/module-interop"; |
| 4 | +import { presetToKernelRules } from "@textlint/runtime-helper" |
| 5 | +import { parseOptionsFromConfig } from "@textlint/config-partial-parser" |
| 6 | +const kernel = new TextlintKernel(); |
| 7 | +const presetRules = [ |
| 8 | + presetToKernelRules(moduleInterop(require('textlint-rule-preset-ja-technical-writing')), 'preset-ja-technical-writing') |
| 9 | +].flat(); |
| 10 | +const rules = []; |
| 11 | +const filterRules = [ |
| 12 | + { |
| 13 | + "ruleId": "comments", |
| 14 | + "rule": moduleInterop(require('textlint-filter-rule-comments')), |
| 15 | + "options": true |
| 16 | + } |
| 17 | +]; |
| 18 | +const plugins = [ |
| 19 | + { |
| 20 | + "pluginId": "@textlint/text", |
| 21 | + "plugin": moduleInterop(require('@textlint/textlint-plugin-text')), |
| 22 | + "options": true |
| 23 | + }, |
| 24 | + { |
| 25 | + "pluginId": "@textlint/markdown", |
| 26 | + "plugin": moduleInterop(require('@textlint/textlint-plugin-markdown')), |
| 27 | + "options": true |
| 28 | + } |
| 29 | +]; |
| 30 | +const allRules = rules.concat(presetRules); |
| 31 | +const config = { |
| 32 | + rules: allRules, |
| 33 | + filterRules: filterRules, |
| 34 | + plugins: plugins |
| 35 | +}; |
| 36 | +const assignConfig = (textlintrc) => { |
| 37 | + const userDefinedConfig = parseOptionsFromConfig(textlintrc); |
| 38 | + if (userDefinedConfig.rules) { |
| 39 | + config.rules = config.rules.map(rule => { |
| 40 | + const override = userDefinedConfig.rules.find(o => o.ruleId === rule.ruleId); |
| 41 | + return { ...rule, ...override }; |
| 42 | + }); |
| 43 | + } |
| 44 | + if (userDefinedConfig.filterRules) { |
| 45 | + config.filterRules = config.filterRules.map(rule => { |
| 46 | + const override = userDefinedConfig.filterRules.find(o => o.ruleId === rule.ruleId); |
| 47 | + return { ...rule, ...override }; |
| 48 | + }); |
| 49 | + } |
| 50 | + if (userDefinedConfig.plugins) { |
| 51 | + config.plugins = config.plugins.map(rule => { |
| 52 | + const override = userDefinedConfig.plugins.find(o => o.pluginId === rule.pluginId); |
| 53 | + return { ...rule, ...override }; |
| 54 | + }); |
| 55 | + } |
| 56 | +}; |
| 57 | +self.addEventListener('message', (event) => { |
| 58 | + const data = event.data; |
| 59 | + const rules = data.ruleId === undefined |
| 60 | + ? config.rules |
| 61 | + : config.rules.filter(rule => rule.ruleId === data.ruleId); |
| 62 | + switch (data.command) { |
| 63 | + case "merge-config": |
| 64 | + return assignConfig(data.textlintrc); |
| 65 | + case "lint": |
| 66 | + return kernel.lintText(data.text, { |
| 67 | + rules: rules, |
| 68 | + filterRules: config.filterRules, |
| 69 | + plugins: config.plugins, |
| 70 | + filePath: "/path/to/README" + data.ext, |
| 71 | + ext: data.ext, |
| 72 | + }).then(result => { |
| 73 | + return self.postMessage({ |
| 74 | + command: "lint:result", |
| 75 | + result |
| 76 | + }); |
| 77 | + }); |
| 78 | + case "fix": |
| 79 | + return kernel.fixText(data.text, { |
| 80 | + rules: rules, |
| 81 | + filterRules: config.filterRules, |
| 82 | + plugins: config.plugins, |
| 83 | + filePath: "/path/to/README" + data.ext, |
| 84 | + ext: data.ext, |
| 85 | + }).then(result => { |
| 86 | + return self.postMessage({ |
| 87 | + command: "fix:result", |
| 88 | + result |
| 89 | + }); |
| 90 | + }); |
| 91 | + default: |
| 92 | + console.log("Unknown command: " + data.command); |
| 93 | + } |
| 94 | +}); |
| 95 | +// ==== |
| 96 | +self.postMessage({ |
| 97 | + command: "init", |
| 98 | + metadata: process.env.TEXTLINT_SCRIPT_METADATA |
| 99 | +}); |
0 commit comments