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 Mermaid support #369

Merged
merged 11 commits into from
Jul 5, 2021
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
6 changes: 5 additions & 1 deletion assets/js/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ var pre = document.getElementsByTagName('pre');
for (var i = 0; i < pre.length; ++ i)
{
var element = pre[i];
element.insertAdjacentHTML('afterbegin', '<button class="btn btn-copy"></button>');
var mermaid = element.getElementsByClassName('language-mermaid')[0];

if (mermaid == null) {
element.insertAdjacentHTML('afterbegin', '<button class="btn btn-copy"></button>');
}
}

var clipboard = new Clipboard('.btn-copy', {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ hljs.registerLanguage('yaml', yaml);
hljs.registerLanguage('md', markdown);

document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('pre code').forEach((block) => {
document.querySelectorAll('pre code:not(.language-mermaid)').forEach((block) => {
hljs.highlightElement(block);
});
});
11 changes: 11 additions & 0 deletions assets/js/mermaid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import mermaid from 'mermaid/dist/mermaid';

var config = {
theme: 'default',
fontFamily: '"Jost", -apple-system, blinkmacsystemfont, "Segoe UI", roboto, "Helvetica Neue", arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";',
};

document.addEventListener('DOMContentLoaded', () => {
mermaid.initialize(config);
mermaid.init(undefined, '.language-mermaid');
});
1 change: 1 addition & 0 deletions assets/scss/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@import "components/comments";
@import "components/forms";
@import "components/images";
@import "components/mermaid";
@import "components/search";
@import "components/tables";
@import "layouts/footer";
Expand Down
15 changes: 10 additions & 5 deletions assets/scss/common/_dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,6 @@ body.dark pre code::-webkit-scrollbar-thumb {
background: $gray-400;
}

body.dark code:not(.hljs) {
background: $body-overlay-dark;
color: $body-color-dark;
}

body.dark pre code:hover {
scrollbar-width: thin;
scrollbar-color: $border-dark transparent;
Expand All @@ -246,6 +241,16 @@ body.dark pre code::-webkit-scrollbar-thumb:hover {
background: $gray-500;
}

body.dark code:not(.hljs):not(.language-mermaid) {
background: $body-overlay-dark;
color: $body-color-dark;
}

body.dark .mermaid,
body.dark pre code.language-mermaid {
background: $white;
}

body.dark blockquote {
border-left: 3px solid $border-dark;
}
Expand Down
4 changes: 4 additions & 0 deletions assets/scss/components/_code.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ pre code:hover {
pre code::-webkit-scrollbar-thumb:hover {
background: $gray-500;
}

code.language-mermaid {
background: none;
}
8 changes: 8 additions & 0 deletions assets/scss/components/_mermaid.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.mermaid {
margin: 1.5rem 0;
padding: 1.5rem;
}

.mermaid svg {
height: auto;
}
7 changes: 7 additions & 0 deletions config/_default/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ rel = "sitemap"
blog = "/blog/:title/"

[module]
[module.hugoVersion]
extended = true
min = "0.80.0"
max = ""
[[module.mounts]]
source = "assets"
target = "assets"
Expand All @@ -89,3 +93,6 @@ rel = "sitemap"
[[module.mounts]]
source = "node_modules/katex"
target = "assets/js/vendor/katex"
[[module.mounts]]
source = "node_modules/mermaid"
target = "assets/js/vendor/mermaid"
1 change: 1 addition & 0 deletions config/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
...whitelister([
'./assets/scss/components/_buttons.scss',
'./assets/scss/components/_code.scss',
'./assets/scss/components/_diagrams.scss',
'./assets/scss/components/_syntax.scss',
'./assets/scss/components/_search.scss',
'./assets/scss/common/_dark.scss',
Expand Down
2 changes: 1 addition & 1 deletion layouts/index.headers
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Security-Policy: default-src 'self'; frame-ancestors https://jamstackthemes.dev; manifest-src 'self'; connect-src 'self'; font-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self'
Content-Security-Policy: default-src 'self'; frame-ancestors https://jamstackthemes.dev; manifest-src 'self'; connect-src 'self'; font-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self' 'unsafe-inline'
X-Frame-Options: SAMEORIGIN
Referrer-Policy: strict-origin
Feature-Policy: geolocation 'self'
Expand Down
9 changes: 9 additions & 0 deletions layouts/partials/footer/script-footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
{{ $katex := resources.Get "js/vendor/katex/dist/katex.js" -}}
{{ $katexAutoRender := resources.Get "js/vendor/katex/dist/contrib/auto-render.js" -}}

{{ $mermaid := resources.Get "js/mermaid.js" | js.Build -}}

{{ $app := resources.Get "js/app.js" -}}

{{ $slice := slice $app -}}
Expand Down Expand Up @@ -63,6 +65,9 @@
<script src="{{ $katexAutoRender.RelPermalink }}" onload="renderMathInElement(document.body);" defer></script>
{{ end -}}
<script src="{{ $js.RelPermalink }}" defer></script>
{{ with .Params.mermaid -}}
<script src="{{ $mermaid.RelPermalink }}" defer></script>
{{ end -}}
{{ if .Site.Params.options.flexSearch -}}
<script src="{{ $index.RelPermalink }}" defer></script>
{{ end -}}
Expand All @@ -73,6 +78,7 @@
{{ $highlight := $highlight | minify | fingerprint "sha512" -}}
{{ $katex := $katex | minify | fingerprint "sha512" -}}
{{ $katexAutoRender := $katexAutoRender | minify | fingerprint "sha512" -}}
{{ $mermaid := $mermaid | minify | fingerprint "sha512" -}}
{{ if .Site.Params.options.bootStrapJs -}}
<script src="{{ $bs.RelPermalink }}" integrity="{{ $bs.Data.Integrity }}" crossorigin="anonymous" defer></script>
{{ end -}}
Expand All @@ -84,6 +90,9 @@
<script src="{{ $katexAutoRender.RelPermalink }}" integrity="{{ $katexAutoRender.Data.Integrity }}" crossorigin="anonymous" defer></script>
{{ end -}}
<script src="{{ $js.RelPermalink }}" integrity="{{ $js.Data.Integrity }}" crossorigin="anonymous" defer></script>
{{ with .Params.mermaid -}}
<script src="{{ $mermaid.RelPermalink }}" integrity="{{ $mermaid.Data.Integrity }}" crossorigin="anonymous" defer></script>
{{ end -}}
{{ if .Site.Params.options.flexSearch -}}
<script src="{{ $index.RelPermalink }}" integrity="{{ $index.Data.Integrity }}" crossorigin="anonymous" defer></script>
{{ end -}}
Expand Down
8 changes: 8 additions & 0 deletions layouts/shortcodes/mermaid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{ if .Page.Params.mermaid -}}
<div class="mermaid{{ with .Get "class" }} {{ . }}{{ end }}">
{{ $data := replaceRE "(^\\s+```)" "" .Inner -}}
{{ replaceRE "(```\\s+$)" "" $data -}}
</div>
{{ else -}}
{{ errorf "Failed to process mermaid shortcode: %s. Set mermaid to true in page front matter." .Position }}
{{ end -}}
11 changes: 6 additions & 5 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
[build.environment]
NODE_VERSION = "16.3.0"
NPM_VERSION = "7.16.0"
HUGO_VERSION = "0.84.4"

[context.production]
command = "exec-bin bin/hugo/hugo --gc --minify"
command = "hugo --gc --minify"

[context.deploy-preview]
command = "exec-bin bin/hugo/hugo --gc --minify -b $DEPLOY_PRIME_URL"
command = "hugo --gc --minify -b $DEPLOY_PRIME_URL"

[context.branch-deploy]
command = "exec-bin bin/hugo/hugo --gc --minify -b $DEPLOY_PRIME_URL"
command = "hugo --gc --minify -b $DEPLOY_PRIME_URL"

[context.next]
command = "exec-bin bin/hugo/hugo --gc --minify"
command = "hugo --gc --minify"

[context.next.environment]
HUGO_ENV = "next"

[dev]
framework = "#custom"
command = "shx rm -rf public resources && exec-bin bin/hugo/hugo server --bind=0.0.0.0 --disableFastRender"
command = "shx rm -rf public resources && hugo server --bind=0.0.0.0 --disableFastRender"
targetPort = 1313
port = 8888
publish = "public"
Expand Down
Loading