feat(#137): adds support for md rendering in webview

This commit is contained in:
tdurieux
2022-10-24 08:34:53 +02:00
parent c62b50ffd0
commit 231b2fe0c8
6 changed files with 75 additions and 13 deletions

View File

@@ -137,22 +137,35 @@ export default class AnonymizedFile {
return this._originalPath;
}
async isFileSupported() {
async extension() {
const filename = basename(await this.originalPath());
const extensions = filename.split(".").reverse();
const extension = extensions[0].toLowerCase();
return extensions[0].toLowerCase();
}
async isImage(): Promise<boolean> {
const extension = await this.extension();
return [
"png",
"jpg",
"jpeg",
"gif",
"svg",
"ico",
"bmp",
"tiff",
"tif",
"webp",
"avif",
"heif",
"heic",
].includes(extension);
}
async isFileSupported() {
const extension = await this.extension();
if (!this.repository.options.pdf && extension == "pdf") {
return false;
}
if (
!this.repository.options.image &&
(extension == "png" ||
extension == "ico" ||
extension == "jpg" ||
extension == "jpeg" ||
extension == "gif")
) {
if (!this.repository.options.image && (await this.isImage())) {
return false;
}
return true;

View File

@@ -4,10 +4,20 @@ import GitHubBase from "./source/GitHubBase";
import { isText } from "istextorbinary";
import { basename } from "path";
import { Transform } from "stream";
import { Readable } from "stream";
const urlRegex =
/<?\b((https?|ftp|file):\/\/)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]\b\/?>?/g;
export function streamToString(stream: Readable): Promise<string> {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
stream.on("error", (err) => reject(err));
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
}
export function isTextFile(filePath: string, content: Buffer) {
const filename = basename(filePath);
const extensions = filename.split(".").reverse();

View File

@@ -5,6 +5,8 @@ import AnonymizedFile from "../AnonymizedFile";
import GitHubDownload from "../source/GitHubDownload";
import AnonymousError from "../AnonymousError";
import { TreeElement } from "../types";
import * as marked from "marked";
import { anonymizeContent, streamToString } from "../anonymize-utils";
const router = express.Router();
@@ -99,7 +101,12 @@ async function webView(req: express.Request, res: express.Response) {
object: f,
});
}
f.send(res);
if ((await f.extension()) == "md") {
const content = await streamToString(await f.anonymizedContent());
res.send(marked.marked(content));
} else {
f.send(res);
}
} catch (error) {
handleError(error, res, req);
}