forked from kolplattformen/skolplattformen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 Trims tag content to fix some markdown issues (kolplattformen#56)
- Loading branch information
1 parent
50ce985
commit f9dc391
Showing
4 changed files
with
156 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |