Skip to content

Commit

Permalink
Fix table export error (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdauth committed May 12, 2021
1 parent ce776a9 commit 2aace15
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
16 changes: 10 additions & 6 deletions utils/src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export const isBrowser = (typeof window !== "undefined");
export const jQuery: JQueryStatic = isBrowser ? require("jquery") : null;
export const cheerio: cheerio.CheerioAPI = isBrowser ? null : eval('require')("cheerio");
export const $: cheerio.CheerioAPI = isBrowser ? jQuery as any : cheerio;
const isBrowser = (typeof window !== "undefined");
const jQuery: JQueryStatic = isBrowser ? require("jquery") : null;
const cheerio: cheerio.CheerioAPI = isBrowser ? null : eval('require')("cheerio");

export function createDiv(): cheerio.Cheerio {
return isBrowser ? jQuery("<div/>") as any : cheerio.load("<div/>").root();
export function createDiv(): [cheerio.Cheerio, cheerio.Root] {
if (isBrowser)
return [jQuery("<div/>"), jQuery] as any;
else {
const $ = cheerio.load("<div/>");
return [$.root(), $];
}
}
33 changes: 8 additions & 25 deletions utils/src/format.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import marked, { MarkedOptions } from 'marked';
import { Field } from "facilmap-types";
import { createDiv, $ } from './dom';
import { createDiv } from './dom';
import { normalizeField } from './filter';
import { quoteHtml } from './utils';
import linkifyStr from 'linkifyjs/string';
import createPurify from 'dompurify';
import { obfuscate } from './obfuscate';

const purify = createPurify(typeof window !== "undefined" ? window : new (eval("require")("jsdom").JSDOM)("").window);

Expand All @@ -28,17 +29,17 @@ export function formatField(field: Field, value: string): string {
}

export function markdownBlock(string: string, options?: MarkedOptions): string {
const ret = createDiv();
const [ret, $] = createDiv();
ret.html(purify.sanitize(marked(string, options)));
applyMarkdownModifications(ret);
applyMarkdownModifications(ret, $);
return ret.html()!;
}

export function markdownInline(string: string, options?: MarkedOptions): string {
const ret = createDiv();
const [ret, $] = createDiv();
ret.html(purify.sanitize(marked(string, options)));
$("p", ret).replaceWith(function(this: cheerio.Element) { return $(this).contents(); });
applyMarkdownModifications(ret);
applyMarkdownModifications(ret, $);
return ret.html()!;
}

Expand All @@ -55,32 +56,14 @@ export function formatTime(seconds: number): string {
return hours + ":" + minutes;
}

function applyMarkdownModifications($el: cheerio.Cheerio): void {
function applyMarkdownModifications($el: cheerio.Cheerio, $: cheerio.Root): void {
$("a[href]", $el).attr({
target: "_blank",
rel: "noopener noreferer"
});


$("a[href^='mailto:']", $el).each(function(this: cheerio.Element) {
const $a = $(this);
let m = $a.attr("href")!.match(/^mailto:(.*)@(.*)$/i);
if(m) {
$a.attr({
href: "#",
"data-u": m[1],
"data-d": m[2]
}).addClass("emobf");
}

m = $a.text().match(/^(.*)@(.*)$/);
if(m && $a.children().length == 0) {
$a.attr({
"data-u2": m[1],
"data-d2": m[2]
}).addClass("emobf2").html("<span>[obfuscated]</span>");
}
});
obfuscate($el, $);
}

export function renderOsmTag(key: string, value: string): string {
Expand Down
5 changes: 2 additions & 3 deletions utils/src/obfuscate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { $ } from "./dom";

export function obfuscate($el: cheerio.Cheerio): void {
$el.find("a[href^='mailto:']").each(function(this: cheerio.Element) {
export function obfuscate($el: cheerio.Cheerio, $: cheerio.Root): void {
$("a[href^='mailto:']", $el).each(function(this: cheerio.Element) {
const $a = $(this);
let m = $a.attr("href")!.match(/^mailto:(.*)@(.*)$/i);
if(m) {
Expand Down

0 comments on commit 2aace15

Please sign in to comment.