Skip to content

Commit

Permalink
feat: highlight code syntax in markdown files
Browse files Browse the repository at this point in the history
fix #1358
  • Loading branch information
lorenzo-cavazzi committed Oct 28, 2021
1 parent dcb5fe3 commit cf7e166
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
35 changes: 32 additions & 3 deletions client/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 client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"redux-thunk": "^2.2.0",
"sass": "^1.37.5",
"showdown": "^1.9.1",
"showdown-highlight": "^2.1.8",
"styled-jsx": "^3.4.5",
"uuid": "^3.3.3",
"xregexp": "^4.4.0",
Expand Down
9 changes: 8 additions & 1 deletion client/src/utils/HelperFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// title.Author: Alex K. - https://stackoverflow.com/users/246342/alex-k
// Source: https://stackoverflow.com/questions/6507056/replace-all-whitespace-characters/6507078#6507078
import showdown from "showdown";
import showdownHighlight from "showdown-highlight";
import DOMPurify from "dompurify";
import XRegExp from "xregexp";

Expand Down Expand Up @@ -166,7 +167,13 @@ function sanitizedHTMLFromMarkdown(markdown, singleLine = false) {
replace: `<${key} $1 class="$3 ${showdownClasses[key]}" $4>`
}));

const converter = new showdown.Converter({ ...showdownOptions, extensions: [...bindings] });
const converter = new showdown.Converter({
...showdownOptions,
extensions: [
...bindings,
showdownHighlight({ pre: true })
]
});
if (singleLine && markdown) {
const lineBreakers = ["<br>", "<br />", "<br/>", "\n"];
const breakPosition = Math.max(...lineBreakers.map(elem => markdown.indexOf(elem)));
Expand Down

0 comments on commit cf7e166

Please sign in to comment.