mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
detect languages removes html tags before detection
This commit is contained in:
parent
3bd8a531f1
commit
5559a9bc06
@ -1,9 +1,17 @@
|
|||||||
import LanguageDetect from 'languagedetect'
|
import LanguageDetect from 'languagedetect'
|
||||||
|
import sanitizeHtml from 'sanitize-html'
|
||||||
|
|
||||||
|
const removeHtmlTags = (input) => {
|
||||||
|
return sanitizeHtml(input, {
|
||||||
|
allowedTags: [],
|
||||||
|
allowedAttributes: {},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const setPostLanguage = (text) => {
|
const setPostLanguage = (text) => {
|
||||||
const lngDetector = new LanguageDetect()
|
const lngDetector = new LanguageDetect()
|
||||||
lngDetector.setLanguageType('iso2')
|
lngDetector.setLanguageType('iso2')
|
||||||
const result = lngDetector.detect(text, 2)
|
const result = lngDetector.detect(removeHtmlTags(text), 2)
|
||||||
return result[0][0]
|
return result[0][0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { getNeode, getDriver } from '../../db/neo4j'
|
|||||||
import createServer from '../../server'
|
import createServer from '../../server'
|
||||||
import { createTestClient } from 'apollo-server-testing'
|
import { createTestClient } from 'apollo-server-testing'
|
||||||
|
|
||||||
|
|
||||||
let mutate
|
let mutate
|
||||||
let authenticatedUser
|
let authenticatedUser
|
||||||
let variables
|
let variables
|
||||||
@ -25,12 +24,10 @@ beforeAll(async () => {
|
|||||||
mutate = createTestClient(server).mutate
|
mutate = createTestClient(server).mutate
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
//await cleanDatabase()
|
// await cleanDatabase()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
const createPostMutation = gql`
|
const createPostMutation = gql`
|
||||||
mutation($title: String!, $content: String!, $categoryIds: [ID]) {
|
mutation($title: String!, $content: String!, $categoryIds: [ID]) {
|
||||||
CreatePost(title: $title, content: $content, categoryIds: $categoryIds) {
|
CreatePost(title: $title, content: $content, categoryIds: $categoryIds) {
|
||||||
@ -55,16 +52,18 @@ describe('languagesMiddleware', () => {
|
|||||||
icon: 'university',
|
icon: 'university',
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('detects German', async () => {
|
it('detects German', async () => {
|
||||||
variables = {
|
variables = {
|
||||||
...variables,
|
...variables,
|
||||||
content: 'Jeder sollte vor seiner eigenen Tür kehren.',
|
content: 'Jeder sollte vor seiner eigenen Tür kehren.',
|
||||||
}
|
}
|
||||||
await expect(mutate({
|
await expect(
|
||||||
mutation: createPostMutation,
|
mutate({
|
||||||
variables,
|
mutation: createPostMutation,
|
||||||
})).resolves.toMatchObject({
|
variables,
|
||||||
|
}),
|
||||||
|
).resolves.toMatchObject({
|
||||||
data: {
|
data: {
|
||||||
CreatePost: {
|
CreatePost: {
|
||||||
language: 'de',
|
language: 'de',
|
||||||
@ -72,16 +71,18 @@ describe('languagesMiddleware', () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('detects English', async () => {
|
it('detects English', async () => {
|
||||||
variables = {
|
variables = {
|
||||||
...variables,
|
...variables,
|
||||||
content: 'A journey of a thousand miles begins with a single step.',
|
content: 'A journey of a thousand miles begins with a single step.',
|
||||||
}
|
}
|
||||||
await expect(mutate({
|
await expect(
|
||||||
mutation: createPostMutation,
|
mutate({
|
||||||
variables,
|
mutation: createPostMutation,
|
||||||
})).resolves.toMatchObject({
|
variables,
|
||||||
|
}),
|
||||||
|
).resolves.toMatchObject({
|
||||||
data: {
|
data: {
|
||||||
CreatePost: {
|
CreatePost: {
|
||||||
language: 'en',
|
language: 'en',
|
||||||
@ -95,15 +96,37 @@ describe('languagesMiddleware', () => {
|
|||||||
...variables,
|
...variables,
|
||||||
content: 'A caballo regalado, no le mires el diente.',
|
content: 'A caballo regalado, no le mires el diente.',
|
||||||
}
|
}
|
||||||
await expect(mutate({
|
await expect(
|
||||||
mutation: createPostMutation,
|
mutate({
|
||||||
variables,
|
mutation: createPostMutation,
|
||||||
})).resolves.toMatchObject({
|
variables,
|
||||||
|
}),
|
||||||
|
).resolves.toMatchObject({
|
||||||
data: {
|
data: {
|
||||||
CreatePost: {
|
CreatePost: {
|
||||||
language: 'es',
|
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',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user