detect languages removes html tags before detection

This commit is contained in:
Moriz Wahl 2021-01-19 03:18:51 +01:00 committed by Ulf Gebhardt
parent 3bd8a531f1
commit 5559a9bc06
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
2 changed files with 51 additions and 20 deletions

View File

@ -1,9 +1,17 @@
import LanguageDetect from 'languagedetect'
import sanitizeHtml from 'sanitize-html'
const removeHtmlTags = (input) => {
return sanitizeHtml(input, {
allowedTags: [],
allowedAttributes: {},
})
}
const setPostLanguage = (text) => {
const lngDetector = new LanguageDetect()
lngDetector.setLanguageType('iso2')
const result = lngDetector.detect(text, 2)
const result = lngDetector.detect(removeHtmlTags(text), 2)
return result[0][0]
}

View File

@ -4,7 +4,6 @@ import { getNeode, getDriver } from '../../db/neo4j'
import createServer from '../../server'
import { createTestClient } from 'apollo-server-testing'
let mutate
let authenticatedUser
let variables
@ -25,12 +24,10 @@ beforeAll(async () => {
mutate = createTestClient(server).mutate
})
afterAll(async () => {
//await cleanDatabase()
// await cleanDatabase()
})
const createPostMutation = gql`
mutation($title: String!, $content: String!, $categoryIds: [ID]) {
CreatePost(title: $title, content: $content, categoryIds: $categoryIds) {
@ -55,16 +52,18 @@ describe('languagesMiddleware', () => {
icon: 'university',
})
})
it('detects German', async () => {
variables = {
...variables,
content: 'Jeder sollte vor seiner eigenen Tür kehren.',
}
await expect(mutate({
mutation: createPostMutation,
variables,
})).resolves.toMatchObject({
await expect(
mutate({
mutation: createPostMutation,
variables,
}),
).resolves.toMatchObject({
data: {
CreatePost: {
language: 'de',
@ -72,16 +71,18 @@ describe('languagesMiddleware', () => {
},
})
})
it('detects English', async () => {
variables = {
...variables,
content: 'A journey of a thousand miles begins with a single step.',
}
await expect(mutate({
mutation: createPostMutation,
variables,
})).resolves.toMatchObject({
await expect(
mutate({
mutation: createPostMutation,
variables,
}),
).resolves.toMatchObject({
data: {
CreatePost: {
language: 'en',
@ -95,15 +96,37 @@ describe('languagesMiddleware', () => {
...variables,
content: 'A caballo regalado, no le mires el diente.',
}
await expect(mutate({
mutation: createPostMutation,
variables,
})).resolves.toMatchObject({
await expect(
mutate({
mutation: createPostMutation,
variables,
}),
).resolves.toMatchObject({
data: {
CreatePost: {
language: 'es',
},
},
})
})
})
it('detects German in between lots of html tags', async () => {
variables = {
...variables,
content:
'<strong>Jeder</strong> <strike>sollte</strike> <strong>vor</strong> <span>seiner</span> eigenen <blockquote>Tür</blockquote> kehren.',
}
await expect(
mutate({
mutation: createPostMutation,
variables,
}),
).resolves.toMatchObject({
data: {
CreatePost: {
language: 'de',
},
},
})
})
})