Skip to content

Commit

Permalink
fix: 🐛 Trims tag content to fix some markdown issues (kolplattformen#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanObrink authored Feb 11, 2021
1 parent 50ce985 commit f9dc391
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 111 deletions.
214 changes: 107 additions & 107 deletions lib/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
],
}
})
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'
}
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 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'
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',
}])
},
],
}
})
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',
}])
})
})
})
})
})
7 changes: 3 additions & 4 deletions lib/parse.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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(),
})
Expand All @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions lib/parseHtml.test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions lib/parseHtml.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f9dc391

Please sign in to comment.