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

feat: Enabled syntax highlight to a code block #83

Merged
merged 1 commit into from
Jul 17, 2020
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
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@types/webscopeio__react-textarea-autocomplete": "4.6.1",
"@webscopeio/react-textarea-autocomplete": "4.6.3",
"dompurify": "2.0.12",
"highlight.js": "10.1.1",
"marked": "1.0.0",
"react": "16.13.1",
"react-dom": "16.13.1"
Expand Down
53 changes: 53 additions & 0 deletions src/app/markdown/renderer/highlight-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const red = "color: #d91e18;";
const orange = "color: #aa5d00;";
const yellow = "color: #ffd700;";
const green = "color: #008000;";
const blue = "color: #007faa;";
const purple = "color: #7928a1;";
const bold = "font-weight: bold;";

export const highlightStyles: { [index: string]: string } = {
"hljs-comment": "color: #696969;",
"hljs-quote": "color: #696969;",
"hljs-meta": orange,
"hljs-meta-keyword": orange,
"hljs-meta-string": blue,
"hljs-variable": red,
"hljs-template-variable": red,
"hljs-tag": red,
"hljs-name": red,
"hljs-selector-id": red,
"hljs-selector-class": red,
"hljs-regexp": red,
"hljs-deletion": red,
"hljs-number": orange,
"hljs-built_in": orange,
"hljs-builtin-name": orange,
"hljs-literal": orange,
"hljs-type": orange,
"hljs-params": "",
"hljs-link": orange,
"hljs-attribute": yellow,
"hljs-string": green,
"hljs-symbol": green,
"hljs-bullet": green,
"hljs-addition": green,
"hljs-title": blue,
"hljs-section": blue,
"hljs-keyword": purple,
"hljs-selector-tag": purple,
"hljs-emphasis": "font-style: italic;",
"hljs-strong": bold,
"hljs-class": bold,
};

export const languageAliases: { [index: string]: string } = {
zsh: "bash",
sh: "bash",
"c++": "cpp",
html: "xml",
js: "javascript",
ts: "typescript",
kt: "kotlin",
yaml: "yml",
};
54 changes: 49 additions & 5 deletions src/app/markdown/renderer/marktone-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { MarkedOptions, Renderer } from "marked";
import MentionReplacer from "../replacer/mention-replacer";
import KintoneClient from "../../kintone/kintone-client";
import hljs from "highlight.js";
import { highlightStyles, languageAliases } from "./highlight-settings";

class MarktoneRendererHelper {
static escapeHTML(html: string): string {
const escapeTest = /[&<>"']/;
const escapeReplace = /[&<>"']/g;
const escapeReplace = new RegExp(escapeTest, "g");
const replacements: { [key: string]: string } = {
"&": "&amp;",
"<": "&lt;",
Expand All @@ -20,6 +22,43 @@ class MarktoneRendererHelper {

return html;
}

static unescapeHTML(html: string): string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

const unescapeTest = /(&(?:lt|amp|gt|quot|#39);)/;
const unescapeReplace = new RegExp(unescapeTest, "g");
const replacements: { [key: string]: string } = {
"&amp;": "&",
"&lt;": "<",
"&gt;": ">",
"&quot;": '"',
"&#39;": "'",
};

if (unescapeTest.test(html)) {
return html.replace(unescapeReplace, (ch) => replacements[ch]);
}

return html;
}

static highlightCode(code: string, specifiedLanguage: string): string {
let language = specifiedLanguage;

if (!hljs.listLanguages().includes(specifiedLanguage)) {
language = languageAliases[specifiedLanguage] || "plaintext";
}
const highlightedCode = hljs.highlight(language, code).value;
const highlightedCodeWithInlineStyle = highlightedCode.replace(
/class="([\w-]+)"/g,
(matchedString, className) => {
const style = highlightStyles[className];
if (style === undefined) return matchedString;
return `style="${style}"`;
}
);

return highlightedCodeWithInlineStyle;
}
}

interface Render {
Expand Down Expand Up @@ -67,15 +106,20 @@ class MarktoneRenderer extends Renderer {
}

code(code: string, language: string, isEscaped: boolean): string {
const escapedCode = isEscaped
? code
: MarktoneRendererHelper.escapeHTML(code);
korosuke613 marked this conversation as resolved.
Show resolved Hide resolved
const unescapedCode = isEscaped
? MarktoneRendererHelper.unescapeHTML(code)
: code;
const escapedCodeWithHighlight = MarktoneRendererHelper.highlightCode(
unescapedCode,
language
);

const preStyle =
"background-color: #f6f8fa; border-radius: 3px; padding: 8px 16px;";
const codeStyle = `font-family: ${this.monospaceFontFamiliesString};`;
console.log(codeStyle);
korosuke613 marked this conversation as resolved.
Show resolved Hide resolved

return `<pre style="${preStyle}"><code style="${codeStyle}">${escapedCode}</code></pre>`;
return `<pre style="${preStyle}"><code style="${codeStyle}">${escapedCodeWithHighlight}</code></pre>`;
}

blockquote(quote: string): string {
Expand Down