Skip to content
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: 6 additions & 0 deletions .changeset/nasty-suits-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hyperbook/markdown": minor
"@hyperbook/types": minor
---

add option to show a qr code to the current page
13 changes: 13 additions & 0 deletions packages/markdown/assets/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ var hyperbook = (function () {
const tocDrawerEl = document.getElementById("toc-drawer");
tocDrawerEl.open = !tocDrawerEl.open;
}

function qrcodeOpen() {
const qrCodeDialog = document.getElementById("qrcode-dialog");
qrCodeDialog.showModal();
}

function qrcodeClose() {
const qrCodeDialog = document.getElementById("qrcode-dialog");
qrCodeDialog.close();
}

function navToggle() {
const navDrawerEl = document.getElementById("nav-drawer");
navDrawerEl.open = !navDrawerEl.open;
Expand Down Expand Up @@ -83,5 +94,7 @@ var hyperbook = (function () {
toggleBookmark,
navToggle,
tocToggle,
qrcodeOpen,
qrcodeClose,
};
})();
89 changes: 86 additions & 3 deletions packages/markdown/assets/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ figure {
background-color: var(--color-text);
}

.hyperbook-markdown #toc-toggle {
.hyperbook-markdown #toc-toggle,
.hyperbook-markdown #qrcode-open {
background: var(--color-background);
border-color: var(--color-nav-border);
cursor: pointer;
Expand Down Expand Up @@ -399,7 +400,84 @@ figure {
text-decoration: underline;
}

.hyperbook-markdown #toc-toggle {
.hyperbook-markdown #qrcode-dialog {
background: var(--color-background);
border: 1px solid var(--color-brand);
width: 100%;
height: 100%;
}

#qrcode-dialog .container {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
padding: 16px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
}

#qrcode-dialog .name {
color: var(--color-text);
text-align: center;
font-size: 2rem;
}

#qrcode-dialog .url {
color: var(--color-text);
text-align: center;
font-size: 1.25rem;
}

#qrcode-dialog .close {
background: none;
border: none;
font-size: 2rem;
color: var(--color-text);
cursor: pointer;
position: absolute;
font: monospace;
top: 0px;
right: 0px;
width: 48px;
height: 48px;
display: flex;
justify-content: center;
align-items: center;
background: var(--color-background);
border-radius: 8px;
}

#qrcode-dialog .close:hover .close-icon {
background-color: var(--color-brand);
}

.hyperbook-markdown #qrcode-dialog svg {
width: 100%;
max-width: 512px;
margin: 0 auto;
}

.hyperbook-markdown .close-icon {
background-color: var(--color-text);
width: 32px;
height: 32px;
mask-image: url('data:image/svg+xml,<svg width="32px" height="32px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M5.29289 5.29289C5.68342 4.90237 6.31658 4.90237 6.70711 5.29289L12 10.5858L17.2929 5.29289C17.6834 4.90237 18.3166 4.90237 18.7071 5.29289C19.0976 5.68342 19.0976 6.31658 18.7071 6.70711L13.4142 12L18.7071 17.2929C19.0976 17.6834 19.0976 18.3166 18.7071 18.7071C18.3166 19.0976 17.6834 19.0976 17.2929 18.7071L12 13.4142L6.70711 18.7071C6.31658 19.0976 5.68342 19.0976 5.29289 18.7071C4.90237 18.3166 4.90237 17.6834 5.29289 17.2929L10.5858 12L5.29289 6.70711C4.90237 6.31658 4.90237 5.68342 5.29289 5.29289Z" fill="%230F1729"/></svg>');
}

.hyperbook-markdown .qrcode-icon {
background-color: var(--color-text);
width: 32px;
height: 32px;
mask-image: url('data:image/svg+xml,<svg width="32px" height="32px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 9h6V3H3zm1-5h4v4H4zm1 1h2v2H5zm10 4h6V3h-6zm1-5h4v4h-4zm1 1h2v2h-2zM3 21h6v-6H3zm1-5h4v4H4zm1 1h2v2H5zm15 2h1v2h-2v-3h1zm0-3h1v1h-1zm0-1v1h-1v-1zm-10 2h1v4h-1v-4zm-4-7v2H4v-1H3v-1h3zm4-3h1v1h-1zm3-3v2h-1V3h2v1zm-3 0h1v1h-1zm10 8h1v2h-2v-1h1zm-1-2v1h-2v2h-2v-1h1v-2h3zm-7 4h-1v-1h-1v-1h2v2zm6 2h1v1h-1zm2-5v1h-1v-1zm-9 3v1h-1v-1zm6 5h1v2h-2v-2zm-3 0h1v1h-1v1h-2v-1h1v-1zm0-1v-1h2v1zm0-5h1v3h-1v1h-1v1h-1v-2h-1v-1h3v-1h-1v-1zm-9 0v1H4v-1zm12 4h-1v-1h1zm1-2h-2v-1h2zM8 10h1v1H8v1h1v2H8v-1H7v1H6v-2h1v-2zm3 0V8h3v3h-2v-1h1V9h-1v1zm0-4h1v1h-1zm-1 4h1v1h-1zm3-3V6h1v1z"/><path fill="none" d="M0 0h24v24H0z"/></svg>');
}

.hyperbook-markdown #toc-toggle,
.hyperbook-markdown #qrcode-open {
position: fixed;
padding: 4px;
top: 90px;
Expand All @@ -413,7 +491,12 @@ figure {
z-index: 1000;
}

.hyperbook-markdown #toc-toggle:hover {
.hyperbook-markdown #qrcode-open {
top: 140px;
}

.hyperbook-markdown #toc-toggle:hover,
.hyperbook-markdown #qrcode-toggle:hover {
opacity: 1;
}

Expand Down
1 change: 1 addition & 0 deletions packages/markdown/devBuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const markdown = await fs.readFile("dev.md", "utf8");

const result = await process(markdown, {
root: "",
qrcode: true,
config: {
name: "Hyperbook Dokumenation",
description: "Dokumentation für Hyperbook erstellt mit Hyperbook",
Expand Down
1 change: 1 addition & 0 deletions packages/markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@rehype-pretty/transformers": "^0.13.2",
"decircular": "^1.0.0",
"handlebars": "^4.7.8",
"hast-util-from-html": "^2.0.3",
"is-obj": "^3.0.0",
"js-base64": "^3.7.7",
"mdast-util-directive": "^3.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/markdown/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import remarkDirectiveStruktog from "./remarkDirectiveStruktog";
import remarkDirectiveTerm from "./remarkDirectiveTerm";
import remarkLink from "./remarkLink";
import remarkDirectivePagelist from "./remarkDirectivePagelist";
import rehypeQrCode from "./rehypeQrCode";

const remark = (ctx: HyperbookContext) => {
const remarkPlugins: PluggableList = [
Expand Down Expand Up @@ -88,6 +89,7 @@ const remark = (ctx: HyperbookContext) => {

const rehypePlugins: PluggableList = [
rehypeTableOfContents(ctx),
rehypeQrCode(ctx),
rehypeKatex,
[
rehypePrettyCode,
Expand Down
127 changes: 127 additions & 0 deletions packages/markdown/src/rehypeQrCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Register directive nodes in mdast:
/// <reference types="mdast-util-directive" />
//
import { HyperbookContext } from "@hyperbook/types";
import { ElementContent, Root } from "hast";
import { VFile } from "vfile";
import QRCode from "qrcode-svg";
import { fromHtml } from "hast-util-from-html";

export default (ctx: HyperbookContext) => () => {
const qrcode = ctx.config.qrcode || ctx.navigation.current?.qrcode || true;
return (tree: Root, file: VFile) => {
const originalChildren = tree.children as ElementContent[];

if (!qrcode || !ctx.navigation.current?.href) {
return;
}

const qr = new QRCode({
content: ctx.navigation.current.href,
padding: 0,
width: 512,
height: 512,
color: "var(--color-text)",
container: "svg-viewbox",
background: "var(--color-background)",
ecl: "M",
}).svg();

const qrcodeDialog: ElementContent[] = [
{
type: "element",
tagName: "button",
properties: {
id: "qrcode-open",
onclick: "hyperbook.qrcodeOpen()",
title: "QR-Code",
},
children: [
{
type: "element",
tagName: "div",
properties: {
class: "qrcode-icon",
},
children: [],
},
],
},
{
type: "element",
tagName: "dialog",
properties: {
id: "qrcode-dialog",
},
children: [
{
type: "element",
tagName: "div",
properties: {
class: "container",
},
children: [
{
type: "element",
tagName: "div",
properties: {
class: "name",
},
children: [
{
type: "text",
value: ctx.navigation.current.name || ctx.config.name,
},
],
},
...(fromHtml(qr).children as any),
{
type: "element",
tagName: "div",
properties: {
class: "url",
},
children: [
{
type: "text",
value: `${ctx.makeUrl(
ctx.navigation.current?.href || "",
"public",
)}`,
},
],
},
],
},
{
type: "element",
tagName: "button",
properties: {
class: "close",
onclick: "hyperbook.qrcodeClose()",
},
children: [
{
type: "element",
tagName: "div",
properties: {
class: "close-icon",
},
children: [],
},
],
},
],
},
];

if (
originalChildren[0].type === "element" &&
originalChildren[0].tagName === "div"
) {
originalChildren[0].children.push(...qrcodeDialog);
}

tree.children = originalChildren;
};
};
4 changes: 4 additions & 0 deletions packages/markdown/src/rehypeTableOfContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default (ctx: HyperbookContext) => () => {
const headings = file.data.headings || [];
const originalChildren = tree.children as ElementContent[];

if (!showToc) {
return;
}

const tocSidebar: ElementContent[] = [
{
type: "element",
Expand Down
Loading