-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
preferences: Render markdown descriptions #10431
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { enableJSDOM } from '../browser/test/jsdom'; | ||
let disableJSDOM = enableJSDOM(); | ||
|
||
import { expect } from 'chai'; | ||
import * as markdownit from 'markdown-it'; | ||
import { MarkdownRenderer } from './markdown-renderer'; | ||
|
||
disableJSDOM(); | ||
|
||
describe('MarkdownRenderer', () => { | ||
|
||
before(() => disableJSDOM = enableJSDOM()); | ||
after(() => disableJSDOM()); | ||
|
||
it('Should render markdown', () => { | ||
const markdownRenderer = new MarkdownRenderer(); | ||
const result = markdownRenderer.renderInline('[title](link)').innerHTML; | ||
expect(result).to.be.equal('<a href="link">title</a>'); | ||
}); | ||
|
||
it('Should accept and use custom engine', () => { | ||
const engine = markdownit(); | ||
const originalTextRenderer = engine.renderer.rules.text!; | ||
engine.renderer.rules.text = (tokens, idx, options, env, self) => `[${originalTextRenderer(tokens, idx, options, env, self)}]`; | ||
const markdownRenderer = new MarkdownRenderer(engine); | ||
const result = markdownRenderer.renderInline('text').innerHTML; | ||
expect(result).to.be.equal('[text]'); | ||
}); | ||
|
||
it('Should modify rendered markdown in place', () => { | ||
const markdownRenderer = new MarkdownRenderer().modify('a', a => { | ||
a.href = 'something-else'; | ||
}); | ||
const result = markdownRenderer.renderInline('[title](link)').innerHTML; | ||
expect(result).to.be.equal('<a href="something-else">title</a>'); | ||
}); | ||
|
||
it('Should modify descendants of children', () => { | ||
const markdownRenderer = new MarkdownRenderer().modify('em', em => { | ||
const strong = document.createElement('strong'); | ||
// eslint-disable-next-line no-unsanitized/property | ||
strong.innerHTML = em.innerHTML; | ||
return strong; | ||
}); | ||
const result = markdownRenderer.render('**bold *bold and italic***').innerHTML; | ||
expect(result).to.be.equal('<p><strong>bold <strong>bold and italic</strong></strong></p>\n'); | ||
}); | ||
|
||
it('Should modify descendants of children after previous modification', () => { | ||
const markdownRenderer = new MarkdownRenderer().modify('em', em => { | ||
const strong = document.createElement('strong'); | ||
// eslint-disable-next-line no-unsanitized/property | ||
strong.innerHTML = em.innerHTML; | ||
return strong; | ||
}).modify('strong', strong => { // Will pick up both the original and modified strong element | ||
const textNode = strong.childNodes.item(0); | ||
textNode.textContent = `changed_${textNode.textContent}`; | ||
}); | ||
const result = markdownRenderer.render('**bold *bold and italic***').innerHTML; | ||
expect(result).to.be.equal('<p><strong>changed_bold <strong>changed_bold and italic</strong></strong></p>\n'); | ||
}); | ||
}); |
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,76 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import * as DOMPurify from 'dompurify'; | ||
import * as markdownit from 'markdown-it'; | ||
|
||
export class MarkdownRenderer { | ||
|
||
protected engine: markdownit; | ||
protected callbacks = new Map<string, ((element: Element) => Element | void)[]>(); | ||
|
||
constructor(engine?: markdownit) { | ||
this.engine = engine ?? markdownit(); | ||
} | ||
|
||
/** | ||
* Adds a modification callback that is applied to every element with the specified tag after rendering to HTML. | ||
* | ||
* @param tag The tag that this modification applies to. | ||
* @param callback The modification to apply on every selected rendered element. Can either modify the element in place or return a new element. | ||
*/ | ||
modify<K extends keyof HTMLElementTagNameMap>(tag: K, callback: (element: HTMLElementTagNameMap[K]) => Element | void): MarkdownRenderer { | ||
if (this.callbacks.has(tag)) { | ||
this.callbacks.get(tag)!.push(callback); | ||
} else { | ||
this.callbacks.set(tag, [callback]); | ||
} | ||
return this; | ||
} | ||
|
||
render(markdown: string): HTMLElement { | ||
return this.renderInternal(this.engine.render(markdown)); | ||
} | ||
|
||
renderInline(markdown: string): HTMLElement { | ||
return this.renderInternal(this.engine.renderInline(markdown)); | ||
} | ||
|
||
protected renderInternal(renderedHtml: string): HTMLElement { | ||
const div = this.sanitizeHtml(renderedHtml); | ||
for (const [tag, calls] of this.callbacks) { | ||
for (const callback of calls) { | ||
const elements = Array.from(div.getElementsByTagName(tag)); | ||
for (const element of elements) { | ||
const result = callback(element); | ||
if (result) { | ||
const parent = element.parentElement; | ||
if (parent) { | ||
parent.replaceChild(result, element); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return div; | ||
} | ||
|
||
protected sanitizeHtml(html: string): HTMLElement { | ||
const div = document.createElement('div'); | ||
div.innerHTML = DOMPurify.sanitize(html); | ||
return div; | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm Ok with adding this, but there is also a utility in the
Preference.TreeNode
namespace (getGroupAndIdFromNodeId
) to get thegroup
andid
(i.e. preferenceId) from the ID of the node.