-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTML content transformer middleware (#5338)
* Add HTML content transformer * Add entry * Fix alt text * Apply to fenced code blocks only * Add breaking changes * Update entry * Add PR * Fix tests * Fix test * Add xmlns and remove HTML content provider related attributes --------- Co-authored-by: Eugene <EOlonov@gmail.com>
- Loading branch information
Showing
25 changed files
with
378 additions
and
207 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
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
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
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
8 changes: 8 additions & 0 deletions
8
packages/bundle/src/markdown/createHTMLContentTransformMiddleware.ts
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,8 @@ | ||
import { type HTMLContentTransformMiddleware } from 'botframework-webchat-component'; | ||
|
||
import createCodeBlockCopyButtonMiddleware from './middleware/createCodeBlockCopyButtonMiddleware'; | ||
import createSanitizeMiddleware from './middleware/createSanitizeMiddleware'; | ||
|
||
export default function createHTMLContentTransformMiddleware(): readonly HTMLContentTransformMiddleware[] { | ||
return Object.freeze([createCodeBlockCopyButtonMiddleware(), createSanitizeMiddleware()]); | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/bundle/src/markdown/middleware/createCodeBlockCopyButtonMiddleware.ts
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,18 @@ | ||
import { type HTMLContentTransformMiddleware } from 'botframework-webchat-component'; | ||
|
||
import codeBlockCopyButtonDocumentMod from '../private/codeBlockCopyButtonDocumentMod'; | ||
|
||
export default function createCodeBlockCopyButtonMiddleware(): HTMLContentTransformMiddleware { | ||
return () => next => request => | ||
next( | ||
Object.freeze({ | ||
...request, | ||
documentFragment: codeBlockCopyButtonDocumentMod(request.documentFragment, { | ||
codeBlockCopyButtonAltCopied: request.codeBlockCopyButtonAltCopied, | ||
codeBlockCopyButtonAltCopy: request.codeBlockCopyButtonAltCopy, | ||
codeBlockCopyButtonClassName: request.codeBlockCopyButtonClassName, | ||
codeBlockCopyButtonTagName: request.codeBlockCopyButtonTagName | ||
}) | ||
}) | ||
); | ||
} |
109 changes: 109 additions & 0 deletions
109
packages/bundle/src/markdown/middleware/createSanitizeMiddleware.ts
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,109 @@ | ||
import { | ||
parseDocumentFragmentFromString, | ||
serializeDocumentFragmentIntoString | ||
} from 'botframework-webchat-component/internal'; | ||
import sanitizeHTML from 'sanitize-html'; | ||
|
||
const BASE_SANITIZE_HTML_OPTIONS = Object.freeze({ | ||
allowedAttributes: { | ||
a: ['aria-label', 'class', 'href', 'name', 'rel', 'target'], | ||
button: ['aria-label', 'class', 'type', 'value'], | ||
img: ['alt', 'aria-label', 'class', 'src', 'title'], | ||
pre: ['class'], | ||
span: ['aria-label'] | ||
}, | ||
allowedSchemes: ['data', 'http', 'https', 'ftp', 'mailto', 'sip', 'tel'], | ||
allowedTags: [ | ||
'a', | ||
'b', | ||
'blockquote', | ||
'br', | ||
'button', | ||
'caption', | ||
'code', | ||
'del', | ||
'div', | ||
'em', | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'h6', | ||
'hr', | ||
'i', | ||
'img', | ||
'ins', | ||
'li', | ||
'nl', | ||
'ol', | ||
'p', | ||
'pre', | ||
's', | ||
'span', | ||
'strike', | ||
'strong', | ||
'table', | ||
'tbody', | ||
'td', | ||
'tfoot', | ||
'th', | ||
'thead', | ||
'tr', | ||
'ul', | ||
|
||
// Followings are for MathML elements, from https://developer.mozilla.org/en-US/docs/Web/MathML. | ||
'annotation-xml', | ||
'annotation', | ||
'math', | ||
'merror', | ||
'mfrac', | ||
'mi', | ||
'mmultiscripts', | ||
'mn', | ||
'mo', | ||
'mover', | ||
'mpadded', | ||
'mphantom', | ||
'mprescripts', | ||
'mroot', | ||
'mrow', | ||
'ms', | ||
'mspace', | ||
'msqrt', | ||
'mstyle', | ||
'msub', | ||
'msubsup', | ||
'msup', | ||
'mtable', | ||
'mtd', | ||
'mtext', | ||
'mtr', | ||
'munder', | ||
'munderover', | ||
'semantics' | ||
], | ||
// Bug of https://github.com/apostrophecms/sanitize-html/issues/633. | ||
// They should not remove `alt=""` even though it is empty. | ||
nonBooleanAttributes: [] | ||
}); | ||
|
||
export default function createSanitizeMiddleware() { | ||
return () => () => request => { | ||
const { codeBlockCopyButtonTagName, documentFragment } = request; | ||
const sanitizeHTMLOptions = { | ||
...BASE_SANITIZE_HTML_OPTIONS, | ||
allowedAttributes: { | ||
...BASE_SANITIZE_HTML_OPTIONS.allowedAttributes, | ||
[codeBlockCopyButtonTagName]: ['class', 'data-alt-copy', 'data-alt-copied', 'data-testid', 'data-value'] | ||
}, | ||
allowedTags: [...BASE_SANITIZE_HTML_OPTIONS.allowedTags, codeBlockCopyButtonTagName] | ||
}; | ||
|
||
const htmlAfterBetterLink = serializeDocumentFragmentIntoString(documentFragment); | ||
|
||
const htmlAfterSanitization = sanitizeHTML(htmlAfterBetterLink, sanitizeHTMLOptions); | ||
|
||
return parseDocumentFragmentFromString(htmlAfterSanitization); | ||
}; | ||
} |
Oops, something went wrong.