-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
117 lines (106 loc) · 3.27 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { app, ipcMain, BrowserWindow, shell } = require('electron');
const { TextLintEngine } = require('textlint');
const { exec } = require('child_process');
const util = require('util');
const path = require('path');
const textract = require('@nosferatu500/textract');
const { execFilePath, resourceFilePath } = require('./lib/binaries');
const configDirPath = resourceFilePath('textlint_config');
const engine = new TextLintEngine({
formatterName: 'json',
configFile: [path.join(configDirPath, 'textlintrc.yml')],
});
async function parseFile(filePath) {
if (filePath.endsWith('.pdf')) {
// 外部コマンド `pdftotext` を実行してpdfをparseする
return util
.promisify(exec)(
`${execFilePath('pdftotext')} -raw -enc UTF-8 "${filePath}" -`,
)
.then((parserOutput) => parserOutput.stdout);
}
// textractライブラリでファイルをparseする
return util
.promisify(textract.fromFileWithPath)(filePath, {
preserveLineBreaks: true,
})
.then((text) => text);
}
function textPreprocess(text) {
return (
text
// 半角カナをNFD -> NFC変換する
.normalize()
// \u2028(Unicodeの'LINE SEPARATOR')があるとtextlintの行数がズレる。消す
.replaceAll('\u2028', '')
// BEL
.replaceAll('\u0007', '')
// Form Feed
.replaceAll('\u000c', '')
// Ideographic Space
.replaceAll('\u3000', '')
);
}
async function textlintExec(text) {
return engine
.executeOnText(text)
.then((results) => engine.formatResults(results))
.then(JSON.parse);
}
function formatTextlintOutput(text, textlintOutput, filePath) {
// textlintOutputの例↓
// [{"messages":[{"type":"lint","ruleId":"ja-no-successive-word","message":"\"を\" が連続して2回使われています。","index":5,"line":1,"column":6,"severity":2}],"filePath":"<text>"}]
const violations = textlintOutput
.flatMap((out) => out.messages)
.map((d) => ({
Message: `${d.message} (${d.ruleId})`,
ViolatedIndices: [{ Start: d.column - 1, Length: 1 }], // textlintはLengthを返してくれないので、1とする
ViolatedSection: text.split('\n')[d.line - 1],
}));
return {
ok: violations.length === 0,
fileName: path.basename(filePath),
results: [
{
RuleName: 'TextLint',
Description: '日本語校正',
Violations: violations,
},
],
};
}
let mainWindow;
app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 1200,
height: 675,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
worldSafeExecuteJavaScript: true,
preload: path.join(__dirname, 'preload.js'),
},
});
mainWindow.loadURL(`file://${__dirname}/index.html`);
mainWindow.on('closed', () => {
mainWindow = null;
});
});
app.on('window-all-closed', () => {
app.quit();
});
ipcMain.handle('proofread', async (_event, filePath) => {
return parseFile(filePath)
.then((text) => textPreprocess(text))
.then((text) =>
textlintExec(text).then((textlintOutput) =>
formatTextlintOutput(text, textlintOutput, filePath),
),
)
.catch((e) => {
return { errorMsg: e };
});
});
ipcMain.on('openTextlintConfig', () => {
shell.openPath(configDirPath);
});