Переглянути джерело

Add a whitelist of words which should not be spellchecked

Shane Kilkelly 9 роки тому
батько
коміт
7842bcd24f

+ 17 - 4
services/spelling/app/coffee/SpellingAPIManager.coffee

@@ -3,20 +3,33 @@ LearnedWordsManager = require './LearnedWordsManager'
 async = require 'async'
 async = require 'async'
 
 
 module.exports = SpellingAPIManager =
 module.exports = SpellingAPIManager =
+
+	wordWhitelist: [
+		'ShareLaTeX',
+		'sharelatex',
+		'LaTeX',
+		'http',
+		'https',
+		'www'
+	]
+
 	runRequest: (token, request, callback = (error, result) ->) ->
 	runRequest: (token, request, callback = (error, result) ->) ->
 		if !request.words?
 		if !request.words?
 			return callback(new Error("malformed JSON"))
 			return callback(new Error("malformed JSON"))
-	
+
 		lang = request.language || "en"
 		lang = request.language || "en"
 
 
 		check = (words, callback) ->
 		check = (words, callback) ->
 			ASpell.checkWords lang, words, (error, misspellings) ->
 			ASpell.checkWords lang, words, (error, misspellings) ->
 				callback error, misspellings: misspellings
 				callback error, misspellings: misspellings
-		
+
+		wordsToCheck = (request.words || []).filter (word) ->
+			SpellingAPIManager.wordWhitelist.indexOf(word) == -1
+
 		if token?
 		if token?
 			LearnedWordsManager.getLearnedWords token, (error, learnedWords) ->
 			LearnedWordsManager.getLearnedWords token, (error, learnedWords) ->
 				return callback(error) if error?
 				return callback(error) if error?
-				words = (request.words || []).slice(0,10000)
+				words = (wordsToCheck).slice(0,10000)
 				check words, (error, result) ->
 				check words, (error, result) ->
 					return callback error if error?
 					return callback error if error?
 					result.misspellings = result.misspellings.filter (m) ->
 					result.misspellings = result.misspellings.filter (m) ->
@@ -24,7 +37,7 @@ module.exports = SpellingAPIManager =
 						learnedWords.indexOf(word) == -1
 						learnedWords.indexOf(word) == -1
 					callback error, result
 					callback error, result
 		else
 		else
-			check(request.words, callback)
+			check(wordsToCheck, callback)
 
 
 	learnWord: (token, request, callback = (error) ->) ->
 	learnWord: (token, request, callback = (error) ->) ->
 		if !request.word?
 		if !request.word?

+ 9 - 0
services/spelling/test/unit/coffee/SpellingAPIManagerTests.coffee

@@ -80,6 +80,15 @@ describe "SpellingAPIManager", ->
 			it "should truncate to 10,000 words", ->
 			it "should truncate to 10,000 words", ->
 				@ASpell.checkWords.calledWith(sinon.match.any, @manyWords.slice(0, 10000)).should.equal true
 				@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 "learnWord", ->
 		describe "without a token", ->
 		describe "without a token", ->
 			beforeEach (done) -> @SpellingAPIManager.learnWord null, word: "banana", (@error) => done()
 			beforeEach (done) -> @SpellingAPIManager.learnWord null, word: "banana", (@error) => done()