Skip to content

Commit

Permalink
chore: docs and changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 11, 2023
1 parent 7c13627 commit 4d5f082
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-cats-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/core": minor
---

Introduces the `decodeHtmlSpecialChars` function.
9 changes: 9 additions & 0 deletions docs/documentation/03- Utilities/sanitization.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ import { SafeHtml } from '@headstartwp/core/react';
<SafeHtml html="<div><p>hello world</p> div content</div>">
```

## decodeHtmlSpeciaChars

This function will decode a pre-defined set of html special chars.

```js
import { decodeHtmlSpeciaChars } from '@headstartwp/core';

decodeHtmlSpeciaChars('Hello world! &#8211; foo bar &#8211');
```
12 changes: 0 additions & 12 deletions packages/core/src/utils/__tests__/decodeHtmlEntities.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/core/src/utils/__tests__/decodeHtmlSpeciaChars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { decodeHtmlSpeciaChars } from '../decodeHtmlSpeciaChars';

describe('decodeHtmlSpeciaChars', () => {
it('decodes html entities', () => {
expect(decodeHtmlSpeciaChars('&#8217;Hello&#8217;')).toBe('’Hello’');
expect(decodeHtmlSpeciaChars('&#8217;Hi &#038; Hello&#8217;')).toBe('’Hi & Hello’');
expect(decodeHtmlSpeciaChars('&#8220;Hi &#038; Hello&#8221;&#8211;Bye')).toBe(
'“Hi & Hello”–Bye',
);
expect(decodeHtmlSpeciaChars('&quot;Hi &amp; Hello&quot;&#8230;Bye')).toBe(
'"Hi & Hello"…Bye',
);

expect(decodeHtmlSpeciaChars('&quot;&lt;Hi &amp; Hello&gt;&quot;&#8230;Bye')).toBe(
'"<Hi & Hello>"…Bye',
);
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Decodes HTML entities
* Decodes HTML special chars entities
*
* @param text The text we want to decode
*
* @returns text with decoded html entities
*/
export function decodeHtmlEntities(text: string) {
export function decodeHtmlSpeciaChars(text: string) {
if (!text) {
return '';
}
Expand All @@ -18,5 +18,7 @@ export function decodeHtmlEntities(text: string) {
.replace(/&#8211;/g, '–')
.replace(/&#8230;/g, '…')
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&');
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
}
2 changes: 1 addition & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * from './errors';
export * from './isInternalLink';
export * from './url';
export * from './log';
export * from './decodeHtmlEntities';
export * from './decodeHtmlSpeciaChars';

0 comments on commit 4d5f082

Please sign in to comment.