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

Fix rendering Mermaid diagrams to PDFs and images #365

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
61 changes: 34 additions & 27 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var url = require('url');
var os = require('os');
var INSTALL_CHECK = false;

function activate(context) {
init();
async function activate(context) {
await init();

var commands = [
vscode.commands.registerCommand('extension.markdown-pdf.settings', async function () { await markdownPdf('settings'); }),
Expand Down Expand Up @@ -397,7 +397,7 @@ function exportPdf(data, filename, type, uri) {
var tmpfilename = path.join(f.dir, f.name + '_tmp.html');
exportHtml(data, tmpfilename);
var options = {
executablePath: vscode.workspace.getConfiguration('markdown-pdf')['executablePath'] || puppeteer.executablePath(),
executablePath: vscode.workspace.getConfiguration('markdown-pdf')['executablePath'] || (await getBundledBrowserPath()),
args: ['--lang='+vscode.env.language, '--no-sandbox', '--disable-setuid-sandbox']
// Setting Up Chrome Linux Sandbox
// https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#setting-up-chrome-linux-sandbox
Expand Down Expand Up @@ -785,7 +785,24 @@ function fixHref(resource, href) {
}
}

function checkPuppeteerBinary() {
async function browserOptions() {
const unresolvedBuildId = require("puppeteer-core/lib/cjs/puppeteer/revisions.js").PUPPETEER_REVISIONS.chrome;
const browsersAPI = require("@puppeteer/browsers");
const browserPlatform = browsersAPI.detectBrowserPlatform();
const buildId = await browsersAPI.resolveBuildId('chrome', browserPlatform, unresolvedBuildId);
return {
browser: 'chrome',
buildId: buildId,
cacheDir: path.join(__dirname, 'browserCache'),
};
}

async function getBundledBrowserPath() {
const browsersAPI = require('@puppeteer/browsers');
return browsersAPI.computeExecutablePath(await browserOptions());
}

async function checkPuppeteerBinary() {
try {
// settings.json
var executablePath = vscode.workspace.getConfiguration('markdown-pdf')['executablePath'] || ''
Expand All @@ -795,8 +812,7 @@ function checkPuppeteerBinary() {
}

// bundled Chromium
const puppeteer = require('puppeteer-core');
executablePath = puppeteer.executablePath();
executablePath = await getBundledBrowserPath();
if (isExistsPath(executablePath)) {
return true;
} else {
Expand All @@ -811,38 +827,29 @@ function checkPuppeteerBinary() {
* puppeteer install.js
* https://github.com/GoogleChrome/puppeteer/blob/master/install.js
*/
function installChromium() {
async function installChromium() {
try {
vscode.window.showInformationMessage('[Markdown PDF] Installing Chromium ...');
var statusbarmessage = vscode.window.setStatusBarMessage('$(markdown) Installing Chromium ...');

// proxy setting
setProxy();

// Remove previous browsers
deleteFile((await browserOptions()).cacheDir);

var StatusbarMessageTimeout = vscode.workspace.getConfiguration('markdown-pdf')['StatusbarMessageTimeout'];
const puppeteer = require('puppeteer-core');
const browserFetcher = puppeteer.createBrowserFetcher();
const revision = require(path.join(__dirname, 'node_modules', 'puppeteer-core', 'package.json')).puppeteer.chromium_revision;
const revisionInfo = browserFetcher.revisionInfo(revision);

// download Chromium
browserFetcher.download(revisionInfo.revision, onProgress)
.then(() => browserFetcher.localRevisions())
.then(onSuccess)
.catch(onError);

function onSuccess(localRevisions) {
console.log('Chromium downloaded to ' + revisionInfo.folderPath);
localRevisions = localRevisions.filter(revision => revision !== revisionInfo.revision);
// Remove previous chromium revisions.
const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision));
const browsersAPI = require('@puppeteer/browsers');
browsersAPI.install({...(await browserOptions()), downloadProgressCallback: onProgress}).then(onSuccess).catch(onError);

function onSuccess(installedBrowser) {
console.log('Chromium downloaded to ' + installedBrowser.path);

if (checkPuppeteerBinary()) {
INSTALL_CHECK = true;
statusbarmessage.dispose();
vscode.window.setStatusBarMessage('$(markdown) Chromium installation succeeded!', StatusbarMessageTimeout);
vscode.window.showInformationMessage('[Markdown PDF] Chromium installation succeeded.');
return Promise.all(cleanupOldVersions);
}
}

Expand Down Expand Up @@ -888,12 +895,12 @@ function setBooleanValue(a, b) {
}
}

function init() {
async function init() {
try {
if (checkPuppeteerBinary()) {
if (await checkPuppeteerBinary()) {
INSTALL_CHECK = true;
} else {
installChromium();
await installChromium();
}
} catch (error) {
showErrorMessage('init()', error);
Expand Down
Loading