generated from atom-community/atom-ide-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from atom-ide-community/float-pane
- Loading branch information
Showing
5 changed files
with
152 additions
and
115 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,93 @@ | ||
import * as React from "react" | ||
import DOMPurify from "dompurify" | ||
import { MarkdownService } from "../../types-packages/main" | ||
import { getMarkdownRenderer } from "../MarkdownRenderer" | ||
|
||
export interface Props { | ||
markdown: Array<string> | string | ||
grammarName?: string | ||
renderer?: MarkdownService | ||
containerClassName: string | ||
contentClassName: string | ||
// already rendered markdown | ||
html?: Array<string> | string | ||
} | ||
|
||
interface State { | ||
markdown: string | ||
} | ||
|
||
/** | ||
* A react component that can hosts markdown texts | ||
*/ | ||
export class MarkdownView extends React.Component<Props, State> { | ||
state: State = { markdown: "" } | ||
|
||
render() { | ||
return ( | ||
<div className={this.props.containerClassName} onWheel={(e) => this.onMouseWheel(e)}> | ||
<div | ||
className={this.props.contentClassName} | ||
dangerouslySetInnerHTML={{ | ||
__html: this.state.markdown, | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
/** | ||
* handles the mouse wheel event to enable scrolling over long text | ||
* @param evt the mouse wheel event being triggered | ||
*/ | ||
onMouseWheel(evt: React.WheelEvent) { | ||
evt.stopPropagation() | ||
} | ||
|
||
/** | ||
Calls `getDocumentationHtml` to convert Markdown to markdown | ||
*/ | ||
async componentDidMount() { | ||
this.setState({ | ||
markdown: (await renderMarkdown(this.props.markdown, this.props.grammarName, this.props.renderer)) ?? "", | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* convert the markdown documentation to markdown | ||
* @param markdownTexts the documentation text in markdown format to be converted | ||
* @param grammarName the default grammar used for embedded code samples | ||
* @param renderer markdown service to be used for rendering | ||
* @return a promise object to track the asynchronous operation | ||
*/ | ||
export async function renderMarkdown( | ||
markdownTexts: Array<string> | string, | ||
grammarName: string = atom.workspace.getActiveTextEditor()?.getGrammar().scopeName?.toLowerCase() || "", | ||
renderer?: MarkdownService | ||
): Promise<string | null> { | ||
if (markdownTexts === undefined) { | ||
return null | ||
} | ||
|
||
let markdownText = "" | ||
// if Array | ||
if (Array.isArray(markdownTexts)) { | ||
if (markdownTexts.length === 0) { | ||
return null | ||
} | ||
markdownText = (markdownTexts as Array<string>).join("\r\n") | ||
} | ||
// if string | ||
else { | ||
//@ts-ignore | ||
markdownText = markdownTexts | ||
} | ||
if (renderer) { | ||
return DOMPurify.sanitize(await renderer.render(markdownText, grammarName)) | ||
} else { | ||
// Use built-in markdown renderer (it already does sanitization) | ||
const render = await getMarkdownRenderer() | ||
return await render(markdownText, grammarName) | ||
} | ||
} |
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