Skip to content
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

Add code formatting to markdown viewer #935

Merged
merged 2 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/CSSReset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ menu, nav, output, ruby, section, summary,
time, mark, audio, video {
border: 0;
/* Consider adding flex-shrink: 0; */
font-size: 100%;
margin: 0;
padding: 0;
vertical-align: baseline;
Expand Down
37 changes: 36 additions & 1 deletion frontend/src/components/viewers/MarkdownViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,44 @@

import * as React from 'react';
import Viewer, { ViewerConfig } from './Viewer';
import { cssRaw } from 'typestyle';
// tslint:disable-next-line:no-var-requires
const markdownIt = require('markdown-it');

cssRaw(`
.markdown-viewer h1,
.markdown-viewer h2,
.markdown-viewer h3,
.markdown-viewer h4,
.markdown-viewer h5,
.markdown-viewer h6 {
position: relative;
margin-top: 1em;
margin-bottom: 16px;
font-size: initial;
font-weight: 700;
line-height: 1.4;
}

.markdown-viewer h1,
.markdown-viewer h2 {
padding-bottom: .3em;
border-bottom: 1px solid #eee;
}

.markdown-viewer code,
.markdown-viewer pre {
background-color: #f8f8f8;
border-radius: 5px;
}

.markdown-viewer pre {
border: solid 1px #eee;
margin: 7px 0;
padding: 7px;
}
`);

export interface MarkdownViewerConfig extends ViewerConfig {
markdownContent: string;
}
Expand All @@ -44,7 +79,7 @@ class MarkdownViewer extends Viewer<MarkdownViewerProps, any> {
return null;
}
const html = markdownIt().render(this._config.markdownContent);
return <div dangerouslySetInnerHTML={{ __html: html }} />;
return <div className='markdown-viewer' dangerouslySetInnerHTML={{ __html: html }} />;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports[`MarkdownViewer renders some basic markdown 1`] = `
}
>
<div
className="markdown-viewer"
dangerouslySetInnerHTML={
Object {
"__html": "<h1>Title</h1>
Expand All @@ -45,6 +46,7 @@ exports[`MarkdownViewer sanitizes the markdown to remove XSS 1`] = `
}
>
<div
className="markdown-viewer"
dangerouslySetInnerHTML={
Object {
"__html": "<pre><code> lower[click me](javascript&amp;#x3a;...)lower
Expand Down