Skip to content

Commit

Permalink
feat: add md and sanitize libs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Aug 9, 2024
1 parent 907836e commit 7150960
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 5 deletions.
133 changes: 133 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/extensions/hooks/notifications-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@
"@directus/types": "^11.2.0",
"@types/chai": "^4.3.17",
"@types/express": "^4.17.21",
"@types/markdown-it": "^14.1.2",
"@types/mocha": "^10.0.7",
"@types/node": "^22.1.0",
"@types/sanitize-html": "^2.11.0",
"@types/sinon": "^17.0.3",
"chai": "^5.1.1",
"express": "^4.19.2",
"mocha": "^10.7.0",
"sinon": "^18.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
},
"dependencies": {
"markdown-it": "^14.1.0",
"sanitize-html": "^2.13.0"
}
}
14 changes: 9 additions & 5 deletions src/extensions/hooks/notifications-format/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { defineHook } from '@directus/extensions-sdk';
import markdownit from 'markdown-it';
import sanitizeHtml from 'sanitize-html';
import type { NextFunction, Request as ExpressRequest, Response } from 'express';
import type { Notification } from '@directus/types';

export type Request = ExpressRequest & {
accountability: {
accountability?: {
customParams?: Record<string, unknown>;
},
};

const md = markdownit();

export default defineHook(({ init, action }) => {
init('middlewares.after', ({ app }) => {
app.use((req: Request, _res: Response, next: NextFunction) => {
Expand All @@ -24,10 +28,10 @@ export default defineHook(({ init, action }) => {
});

action('notifications.read', ({ payload }, { accountability }) => {
if ((accountability as Request['accountability']).customParams?.format) {
payload.forEach((notificaton: Notification) => {
if (notificaton.message) {
notificaton.message = `<div>${notificaton.message}</div>`;
if ((accountability as Request['accountability'])?.customParams?.format === 'html') {
payload.forEach((notification: Notification) => {
if (notification.message) {
notification.message = sanitizeHtml(md.render(notification.message));
}
});
}
Expand Down

0 comments on commit 7150960

Please sign in to comment.