From 7842bcd24f4d8165906c089aa0cb96f705ad65f7 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 22 Jun 2017 09:23:47 +0100 Subject: [PATCH] Add a whitelist of words which should not be spellchecked --- .../app/coffee/SpellingAPIManager.coffee | 21 +++++++++++++++---- .../coffee/SpellingAPIManagerTests.coffee | 9 ++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/services/spelling/app/coffee/SpellingAPIManager.coffee b/services/spelling/app/coffee/SpellingAPIManager.coffee index cb6b4fa326..3a0f68d81b 100644 --- a/services/spelling/app/coffee/SpellingAPIManager.coffee +++ b/services/spelling/app/coffee/SpellingAPIManager.coffee @@ -3,20 +3,33 @@ LearnedWordsManager = require './LearnedWordsManager' async = require 'async' module.exports = SpellingAPIManager = + + wordWhitelist: [ + 'ShareLaTeX', + 'sharelatex', + 'LaTeX', + 'http', + 'https', + 'www' + ] + runRequest: (token, request, callback = (error, result) ->) -> if !request.words? return callback(new Error("malformed JSON")) - + lang = request.language || "en" check = (words, callback) -> ASpell.checkWords lang, words, (error, misspellings) -> callback error, misspellings: misspellings - + + wordsToCheck = (request.words || []).filter (word) -> + SpellingAPIManager.wordWhitelist.indexOf(word) == -1 + if token? LearnedWordsManager.getLearnedWords token, (error, learnedWords) -> return callback(error) if error? - words = (request.words || []).slice(0,10000) + words = (wordsToCheck).slice(0,10000) check words, (error, result) -> return callback error if error? result.misspellings = result.misspellings.filter (m) -> @@ -24,7 +37,7 @@ module.exports = SpellingAPIManager = learnedWords.indexOf(word) == -1 callback error, result else - check(request.words, callback) + check(wordsToCheck, callback) learnWord: (token, request, callback = (error) ->) -> if !request.word? diff --git a/services/spelling/test/unit/coffee/SpellingAPIManagerTests.coffee b/services/spelling/test/unit/coffee/SpellingAPIManagerTests.coffee index 97c43fa07b..2f55d13df0 100644 --- a/services/spelling/test/unit/coffee/SpellingAPIManagerTests.coffee +++ b/services/spelling/test/unit/coffee/SpellingAPIManagerTests.coffee @@ -80,6 +80,15 @@ describe "SpellingAPIManager", -> it "should truncate to 10,000 words", -> @ASpell.checkWords.calledWith(sinon.match.any, @manyWords.slice(0, 10000)).should.equal true + describe 'with words from the whitelist', -> + beforeEach (done) -> + @whitelistWord = @SpellingAPIManager.wordWhitelist[0] + @words = ["One", @whitelistWord, "Two"] + @SpellingAPIManager.runRequest @token, words: @words, (error, @result) => done() + + it 'should ignore the white-listed word', -> + expect(@ASpell.checkWords.lastCall.args[1]).to.deep.equal ["One", "Two"] + describe "learnWord", -> describe "without a token", -> beforeEach (done) -> @SpellingAPIManager.learnWord null, word: "banana", (@error) => done()