Skip to content

Commit 046e3ff

Browse files
authored
Add HTML message rendering support (#22)
Sanitize and render HTML in messages, as the avail bustracker does. Closes #21
1 parent b0bc020 commit 046e3ff

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"@fontsource/open-sans": "*",
1515
"@vitejs/plugin-react": "*",
16+
"dompurify": "*",
1617
"prop-types": "*",
1718
"react": "*",
1819
"react-dom": "*",

src/components/PublicMessage.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import DOMPurify from 'dompurify';
12
import PropTypes from 'prop-types';
23
import classNames from './PublicMessage.module.css';
34

45
/**
56
* Component that displays a message within a {@link PublicMessageBoard}.
67
*
78
* @constructor
8-
* @param {String} message - the text content of the message to be displayed.
9+
* @param {String} message - the text content of the message to be displayed (HTML Supported).
910
* @param {Number} priority - the "priority" level specified by Avail.
1011
* @param {Array} routes - list of routes affected.
1112
* @return {JSX.Element}
@@ -32,9 +33,8 @@ export default function PublicMessage({message, priority, routes}) {
3233
<td
3334
className={`${classNames['priority']} ${classNames[`priority-${priority}`]}`}
3435
colSpan={(routes) ? 1 : 2}
35-
>
36-
{message}
37-
</td>
36+
dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(message)}}
37+
/>
3838
</tr>
3939
);
4040
}

src/hooks/usePublicMessages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {useCallback, useEffect, useMemo, useState} from 'react';
33
/**
44
* @typedef PublicMessageObject
55
* @property {Number} id - a unique id for the message, from InfoPoint
6-
* @property {String} message - the text for a public message.
6+
* @property {String} message - the text for a public message (HTML Supported).
77
* @property {Number} priority - the priority of the message specified by Avail.
88
* @property {[RouteObject]|null} routes - list of routes affected by this message, null if message is general.
99
* @property {Number|null} sortOrder - a pre-set sort order determined by the routes, if applicable.

0 commit comments

Comments
 (0)