Skip to content

Commit

Permalink
add file
Browse files Browse the repository at this point in the history
  • Loading branch information
cscheid committed Mar 10, 2024
1 parent e6a2051 commit d56c049
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/core/handlers/qrcode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* qrcode.ts
*
* Copyright (C) 2022-2024 Posit Software, PBC
*/

import { LanguageCellHandlerContext, LanguageHandler } from "./types.ts";
import { baseHandler, install } from "./base.ts";
import { asMappedString, MappedString } from "../lib/mapped-text.ts";

import { DirectiveCell } from "../lib/break-quarto-md-types.ts";

import QRCode from "qrcode";

const qrHandler: LanguageHandler = {
...baseHandler,

languageName: "qr",

type: "directive",
stage: "pre-engine",

async directive(
_handlerContext: LanguageCellHandlerContext,
directive: DirectiveCell,
): Promise<MappedString> {
const param = directive.shortcode.params[0];

const dataUrl = await (new Promise((resolve, reject) => {
// deno-lint-ignore no-explicit-any
QRCode.toDataURL(param, (err: any, url: any) => {
if (err) {
reject(err);
} else {
resolve(url);
}
});
}));

let opts = directive.shortcode.rawParams.slice(1).map((param) => {
if (param.name) {
return ` ${param.name}="${param.value}"`;
} else {
return ` ${param.value}`;
}
}).join("");
return asMappedString(`![](${dataUrl}){.qrcode${opts}}`);
},
};

install(qrHandler);

0 comments on commit d56c049

Please sign in to comment.