fix: 🐛 Trims tag content to fix some markdown issues (#56)

This commit is contained in:
Johan Öbrink 2021-02-11 13:02:53 +01:00 committed by GitHub
parent 50ce985edc
commit f9dc39128e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 113 deletions

View File

@ -342,123 +342,123 @@ describe('parse', () => {
}
}
})
it('parses news details (except body) correctly', () => {
const item = parse.newsItemDetails(response)
it('parses news details (except body) correctly', () => {
const item = parse.newsItemDetails(response)
expect(item.id).toEqual('123')
expect(item.header).toEqual('Avlusningsdagarna 5-7 februari 2021')
expect(item.imageUrl).toEqual('123123.jpeg')
expect(item.intro).toEqual('Kära vårdnadshavare!I helgen är det avlusningsdagar!')
expect(item.published).toEqual('2021-02-04T14:31:00.000+01:00')
expect(item.modified).toEqual('2021-02-14T14:37:00.000+01:00')
expect(item.author).toEqual('Tieto Evry')
})
it('parses body correctly', () => {
const item = parse.newsItemDetails(response)
expect(item.id).toEqual('123')
expect(item.header).toEqual('Avlusningsdagarna 5-7 februari 2021')
expect(item.imageUrl).toEqual('123123.jpeg')
expect(item.intro).toEqual('Kära vårdnadshavare!I helgen är det avlusningsdagar!')
expect(item.published).toEqual('2021-02-04T14:31:00.000+01:00')
expect(item.modified).toEqual('2021-02-14T14:37:00.000+01:00')
expect(item.author).toEqual('Tieto Evry')
})
it('parses body correctly', () => {
const item = parse.newsItemDetails(response)
const expected = '[1177 hemsida ](https://www.1177.se/sjukdomar--besvar/hud-har-och-naglar/harbotten-och-harsackar/huvudloss/)'
expect(item.body).toContain(expected)
const expected = '[1177 hemsida](https://www.1177.se/sjukdomar--besvar/hud-har-och-naglar/harbotten-och-harsackar/huvudloss/)'
expect(item.body).toContain(expected)
})
})
})
describe('menu', () => {
beforeEach(() => {
response = {
Success: true,
Error: null,
Data: [
{
Title: 'Måndag - Vecka 52',
Description: 'Körrfärsrätt .<br/>Veg färs'
},
],
}
describe('menu', () => {
beforeEach(() => {
response = {
Success: true,
Error: null,
Data: [
{
Title: 'Måndag - Vecka 52',
Description: 'Körrfärsrätt .<br/>Veg färs'
},
],
}
})
it('parses menu correctly', () => {
expect(parse.menu(response)).toEqual([{
title: 'Måndag - Vecka 52',
description: 'Körrfärsrätt .\nVeg färs'
}])
})
})
it('parses menu correctly', () => {
expect(parse.menu(response)).toEqual([{
title: 'Måndag - Vecka 52',
description: 'Körrfärsrätt .\nVeg färs'
}])
describe('user', () => {
let userResponse: any
beforeEach(() => {
userResponse = {
socialSecurityNumber: '197106171635',
isAuthenticated: true,
userFirstName: 'Per-Ola',
userLastName: 'Assarsson',
userEmail: 'per-ola.assarsson@dodgit.com',
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04'
}
})
it('parses user correctly', () => {
expect(parse.user(userResponse)).toEqual({
personalNumber: '197106171635',
firstName: 'Per-Ola',
lastName: 'Assarsson',
email: 'per-ola.assarsson@dodgit.com',
isAuthenticated: true,
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04',
})
})
})
})
describe('user', () => {
let userResponse: any
beforeEach(() => {
userResponse = {
socialSecurityNumber: '197106171635',
isAuthenticated: true,
userFirstName: 'Per-Ola',
userLastName: 'Assarsson',
userEmail: 'per-ola.assarsson@dodgit.com',
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04'
}
})
it('parses user correctly', () => {
expect(parse.user(userResponse)).toEqual({
personalNumber: '197106171635',
firstName: 'Per-Ola',
lastName: 'Assarsson',
email: 'per-ola.assarsson@dodgit.com',
isAuthenticated: true,
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04',
describe('notifications', () => {
beforeEach(() => {
response = {
Success: true,
Error: null,
Data: [
{
Notification: {
Messageid: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
Messagecorrelationid: 'BB54DC8E-BB02-49A5-9806-4A2433031AA7',
Message: '{"messages":{"message":{"messageid":"E2E3A567-307F-4859-91BA-31B1F4522A7B","messagecorrelationid":"BB54DC8E-BB02-49A5-9806-4A2433031AA7","messagetext":"Betygen är publicerade.","messagesubject":"Betyg klara","messagetime":"2020-12-18T15:59:43.195","linkbackurl":"https://elevdokumentation.stockholm.se/loa3/gradesStudent.do","sender":{"name":"Elevdokumentation"},"recipient":{"recipient":"195709227283","role":"Guardian"},"messagetype":{"type":"webnotify"},"system":"Elevdokumentation","participant":"BB7DE89D-D714-4EB2-85CD-36F9991E7C34"}}}',
Readreceipt: false,
Recipient: '195709227283',
Id: 5880387,
DateCreated: '2020-12-18T15:59:46.34',
DateModified: '/Date(1608307186340)/',
Role: 'Guardian',
Participant: 'BB7DE89D-D714-4EB2-85CD-36F9991E7C34'
},
NotificationMessage: {
Messages: {
Message: {
Messageid: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
Messagecorrelationid: 'BB54DC8E-BB02-49A5-9806-4A2433031AA7',
Messagetext: 'Betygen är publicerade.',
Messagetime: '/Date(1608303583195)/',
Linkbackurl: 'https://elevdokumentation.stockholm.se/loa3/gradesStudent.do',
Category: null,
Sender: { Name: 'Elevdokumentation' },
Recipient: {
RecipientRecipient: '195709227283',
Role: 'Guardian',
Schooltype: null
},
Messagetype: { Type: 'webnotify' },
System: 'Elevdokumentation'
}
}
}
},
],
}
})
it('parses notifications correctly', () => {
expect(parse.notifications(response)).toEqual([{
id: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
message: 'Betygen är publicerade.',
sender: 'Elevdokumentation',
url: 'https://elevdokumentation.stockholm.se/loa3/gradesStudent.do',
dateCreated: '2020-12-18T15:59:46.340+01:00',
category: null,
type: 'webnotify',
}])
})
})
})
describe('notifications', () => {
beforeEach(() => {
response = {
Success: true,
Error: null,
Data: [
{
Notification: {
Messageid: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
Messagecorrelationid: 'BB54DC8E-BB02-49A5-9806-4A2433031AA7',
Message: '{"messages":{"message":{"messageid":"E2E3A567-307F-4859-91BA-31B1F4522A7B","messagecorrelationid":"BB54DC8E-BB02-49A5-9806-4A2433031AA7","messagetext":"Betygen är publicerade.","messagesubject":"Betyg klara","messagetime":"2020-12-18T15:59:43.195","linkbackurl":"https://elevdokumentation.stockholm.se/loa3/gradesStudent.do","sender":{"name":"Elevdokumentation"},"recipient":{"recipient":"195709227283","role":"Guardian"},"messagetype":{"type":"webnotify"},"system":"Elevdokumentation","participant":"BB7DE89D-D714-4EB2-85CD-36F9991E7C34"}}}',
Readreceipt: false,
Recipient: '195709227283',
Id: 5880387,
DateCreated: '2020-12-18T15:59:46.34',
DateModified: '/Date(1608307186340)/',
Role: 'Guardian',
Participant: 'BB7DE89D-D714-4EB2-85CD-36F9991E7C34'
},
NotificationMessage: {
Messages: {
Message: {
Messageid: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
Messagecorrelationid: 'BB54DC8E-BB02-49A5-9806-4A2433031AA7',
Messagetext: 'Betygen är publicerade.',
Messagetime: '/Date(1608303583195)/',
Linkbackurl: 'https://elevdokumentation.stockholm.se/loa3/gradesStudent.do',
Category: null,
Sender: { Name: 'Elevdokumentation' },
Recipient: {
RecipientRecipient: '195709227283',
Role: 'Guardian',
Schooltype: null
},
Messagetype: { Type: 'webnotify' },
System: 'Elevdokumentation'
}
}
}
},
],
}
})
it('parses notifications correctly', () => {
expect(parse.notifications(response)).toEqual([{
id: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
message: 'Betygen är publicerade.',
sender: 'Elevdokumentation',
url: 'https://elevdokumentation.stockholm.se/loa3/gradesStudent.do',
dateCreated: '2020-12-18T15:59:46.340+01:00',
category: null,
type: 'webnotify',
}])
})
})
})
})

View File

@ -1,6 +1,5 @@
import { DateTime, DateTimeOptions } from 'luxon'
import * as h2m from 'h2m'
import { htmlDecode } from 'js-htmlencode'
import { toMarkdown } from './parseHtml'
import {
CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem, User, Notification,
} from './types'
@ -89,7 +88,7 @@ export const newsItem = ({
imageUrl: bannerImageUrl,
fullImageUrl: `${IMAGE_HOST}${bannerImageUrl}`,
imageAltText: altText,
body: htmlDecode(h2m(body)),
body: toMarkdown(body),
published: DateTime.fromFormat(pubDateSe, 'd LLLL yyyy HH:mm', dateTimeOptions).toISO(),
modified: DateTime.fromFormat(modDateSe, 'd LLLL yyyy HH:mm', dateTimeOptions).toISO(),
})
@ -114,7 +113,7 @@ export const menuItem = ({
title, description,
}: any): MenuItem => ({
title,
description: htmlDecode(h2m(description)),
description: toMarkdown(description),
})
export const menu = (data: any): MenuItem[] => etjanst(data).map(menuItem)

29
lib/parseHtml.test.ts Normal file
View File

@ -0,0 +1,29 @@
import { trim, toMarkdown } from './parseHtml'
describe('parseHtml', () => {
describe('trim', () => {
it('trims tag content', () => {
const html = `<div>
<b>Hello </b>
<i> World</i>
<u> ! </u>
</div>`
expect(trim(html)).toEqual('<div><b>Hello</b><i>World</i><u>!</u></div>')
})
})
describe('toMarkdown', () => {
it('turns html into Markdown', () => {
const html = `<div>
<h1>Hello </h1>
<strong> World</strong>
<ul>
<li> Foo </li>
</ul>
</div>`
const expected = '# Hello\n**World**\n- Foo'
expect(toMarkdown(html)).toEqual(expected)
})
})
})

17
lib/parseHtml.ts Normal file
View File

@ -0,0 +1,17 @@
import * as h2m from 'h2m'
import { htmlDecode } from 'js-htmlencode'
export const trim = (html: string): string => html
.split('>')
.map((token) => token.trim())
.join('>')
.split('</')
.map((token) => token.trim())
.join('</')
export const toMarkdown = (html: string): string => {
const trimmed = trim(html)
const markdown = h2m(trimmed)
const decoded = htmlDecode(markdown)
return decoded
}