From 6fef74803941d52903c761023ca74d5264d20b89 Mon Sep 17 00:00:00 2001 From: CismonX Date: Mon, 22 Feb 2021 15:06:05 +0800 Subject: [PATCH] Support custom CSS for preview. --- package.json | 5 +++++ src/options.ts | 4 ++++ src/utils/converter.ts | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/package.json b/package.json index dd3a38f..3e8b994 100644 --- a/package.json +++ b/package.json @@ -179,6 +179,11 @@ "type": "boolean", "default": false, "markdownDescription": "Display images in in preview.\n\nIf image display is not needed, turn off this option to improve preview performance." + }, + "texinfo.preview.customCSS": { + "type": "string", + "default": "", + "markdownDescription": "URI of custom CSS for preview.\n\nA good example is https://www.gnu.org/software/gnulib/manual.css" } } }, diff --git a/src/options.ts b/src/options.ts index 6405ce7..29d9e24 100644 --- a/src/options.ts +++ b/src/options.ts @@ -64,6 +64,10 @@ export default class Options implements vscode.Disposable { return Options.instance.getBoolean('preview.displayImage'); } + static get customCSS() { + return Options.instance.getString('preview.customCSS'); + } + static clear() { Options.singleton = undefined; } diff --git a/src/utils/converter.ts b/src/utils/converter.ts index 64548a7..6bb3144 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -5,6 +5,8 @@ * @license MIT */ +import * as vscode from 'vscode'; +import Logger from '../logger'; import Options from '../options'; import DOM from './dom'; import { exec } from './misc'; @@ -20,6 +22,25 @@ export default class Converter { */ private readonly options = ['-o', '-', '--no-split', '--html']; + private includeCustomCSS(cssFileURI: string) { + try { + const uri = vscode.Uri.parse(cssFileURI, true); + switch (uri.scheme) { + case 'file': + this.options.push(`--css-include=${uri.path}`); + break; + case 'http': + case 'https': + this.options.push(`--css-ref=${uri.toString()}`); + break; + default: + throw URIError; + } + } catch (e) { + Logger.log(`Cannot load custom CSS. Invalid URI: '${cssFileURI}'`); + } + } + constructor( private readonly path: string, private readonly imgTransformer?: Operator, @@ -29,6 +50,7 @@ export default class Converter { Options.force && this.options.push('--force'); Options.noValidation && this.options.push('--no-validate'); Options.noWarnings && this.options.push('--no-warn'); + Options.customCSS && this.includeCustomCSS(Options.customCSS); this.options.push(`--error-limit=${Options.errorLimit}`); }