Upload files to "/"
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
// Offline spell checker for wikitext. Markup that is not prose (templates, tags, link targets,
|
||||
// URLs, attributes, comments, code) is blanked out first so it is never flagged.
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const nspell = require('nspell');
|
||||
|
||||
const LANGS = { 'en-US': 1, 'en-GB': 1 };
|
||||
const cache = {};
|
||||
|
||||
function speller(lang) {
|
||||
if (!LANGS[lang]) lang = 'en-US';
|
||||
if (!cache[lang]) {
|
||||
const dir = path.join(__dirname, 'dict', lang);
|
||||
cache[lang] = nspell({ aff: fs.readFileSync(path.join(dir, 'index.aff')), dic: fs.readFileSync(path.join(dir, 'index.dic')) });
|
||||
}
|
||||
return cache[lang];
|
||||
}
|
||||
|
||||
function proseMask(text) {
|
||||
const out = text.split('');
|
||||
const blank = (a, b) => { for (let i = a; i < b; i++) if (out[i] !== '\n') out[i] = ' '; };
|
||||
const kill = (re) => { let m; const s = out.join(''); while ((m = re.exec(s))) blank(m.index, m.index + m[0].length); };
|
||||
kill(/<!--[\s\S]*?(-->|$)/g);
|
||||
kill(/<(nowiki|pre|source|syntaxhighlight|math|code|score|kbd)\b[^>]*>[\s\S]*?<\/\1>/gi);
|
||||
// templates, innermost out
|
||||
let s = out.join('');
|
||||
let depth = 0;
|
||||
let st = 0;
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
if (s[i] === '{' && s[i + 1] === '{') { if (depth === 0) st = i; depth++; i++; }
|
||||
else if (s[i] === '}' && s[i + 1] === '}' && depth > 0) { depth--; i++; if (depth === 0) blank(st, i + 1); }
|
||||
}
|
||||
if (depth > 0) blank(st, out.length);
|
||||
kill(/\[\[(?:File|Image|Media|Category):[^\]\n]*\]\]/gi);
|
||||
// link targets: keep the shown text of [[target|shown]], hide target of [[target]] only when piped
|
||||
kill(/\[\[[^\]|\n]*\|/g);
|
||||
kill(/\[\[[^\]\n]*\]\]/g); // unpiped links are names; skip them
|
||||
kill(/\[(?:https?|ftp):\/\/\S+/gi);
|
||||
kill(/(?:https?|ftp):\/\/\S+/gi);
|
||||
kill(/<\/?[A-Za-z][^>\n]*>/g);
|
||||
kill(/&#?\w+;/g);
|
||||
kill(/[A-Za-z_:-]+\s*=\s*"[^"\n]*"/g);
|
||||
kill(/[A-Za-z_:-]+\s*=\s*[^\s|!"'>]+/g);
|
||||
kill(/__[A-Z]+__/g);
|
||||
kill(/^\{\|.*$/gm);
|
||||
kill(/^\|[-+}]?.*(?:class|style)=.*$/gm);
|
||||
return out.join('');
|
||||
}
|
||||
|
||||
function check(text, lang, personal, ignore) {
|
||||
const sp = speller(lang);
|
||||
(personal || []).forEach((w) => sp.add(w));
|
||||
const ign = new Set((ignore || []).map((w) => w.toLowerCase()));
|
||||
const masked = proseMask(text);
|
||||
const out = [];
|
||||
const re = /[\p{L}][\p{L}'’]*/gu;
|
||||
let m;
|
||||
while ((m = re.exec(masked))) {
|
||||
let w = m[0].replace(/[’]/g, "'");
|
||||
let len = m[0].length;
|
||||
w = w.replace(/'+$/, '');
|
||||
len = w.length;
|
||||
if (w.length < 2) continue;
|
||||
if (/^[A-Z]{2,}s?$/.test(w)) continue; // acronyms
|
||||
if (/[a-z][A-Z]/.test(w)) continue; // camelCase
|
||||
if (ign.has(w.toLowerCase())) continue;
|
||||
if (sp.correct(w)) continue;
|
||||
if (/'s$/i.test(w) && sp.correct(w.slice(0, -2))) continue;
|
||||
if (/^[A-Z]/.test(w) === false && sp.correct(w.charAt(0).toUpperCase() + w.slice(1))) continue;
|
||||
out.push({ start: m.index, end: m.index + len, word: w });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function suggest(word, lang) {
|
||||
return speller(lang).suggest(word).slice(0, 8);
|
||||
}
|
||||
|
||||
module.exports = { check, suggest, proseMask };
|
||||
Reference in New Issue
Block a user