Skip to content

Commit

Permalink
fix(baseliner-md): change html beatifier package
Browse files Browse the repository at this point in the history
html-beautify changed to clean-html, because second has much more readable output, also fixed some
issues with several html outputs and empty default export
  • Loading branch information
Igmat committed May 29, 2018
1 parent 826a682 commit 0078675
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/baset-baseliner-md/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"baset-baseliner-json": "^0.12.0",
"baset-core": "^0.12.0",
"html-beautify": "^1.0.4",
"clean-html": "^1.5.0",
"markdown": "^0.5.0",
"pixelmatch": "^4.0.2",
"pngjs": "^3.3.2"
Expand Down
42 changes: 27 additions & 15 deletions packages/baset-baseliner-md/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import JSONBaseliner from 'baset-baseliner-json';
import { AbstractBaseliner, circularReference, dataTypes, utils } from 'baset-core';
import htmlBeautify from 'html-beautify';
import { clean } from 'clean-html';
import { markdown } from 'markdown';
import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
import { isPrimitive } from 'util';

async function htmlBeautify(src: string) {
return new Promise(resolve => clean(src, resolve));
}
interface IKnownType {
type: typeof dataTypes.image | typeof dataTypes.html;
name: string;
src: string;
}
function uglifyJson(json: string) {
return utils.normalizeEndings(json).split('\n').map(line => line.trim()).join('');
}
function isEmptyJsonSting(json: string) {
return !json || json === '[]' || json === '{}';
return !json || json === '[]' || json === '{}' || uglifyJson(json) === '{"default": {}}';
}

const knownTypeTemplates = {
Expand All @@ -38,7 +44,7 @@ ${json}

const mdTemplate = (json: string, known: IKnownType[]) => `
${!isEmptyJsonSting(json) ? renderJson(json) : ''}
${known.map(renderKnown)}
${known.map(renderKnown).join('')}
`;

export default class MDBaseliner extends AbstractBaseliner {
Expand All @@ -51,20 +57,23 @@ export default class MDBaseliner extends AbstractBaseliner {
create = async (result: Promise<any>[]) => {
const results = await Promise.all(result);
const known = results.length === 1
? this.cutKnownTypes(results[0], 'exports')
: this.cutKnownTypes(results, 'exports');
? await this.cutKnownTypes(results[0], 'exports')
: await this.cutKnownTypes(results, 'exports');

return mdTemplate(await this.jsonBaseliner.create(results), known);
}
compare = async (result: Promise<any>[], baseline: Promise<string>) => {
const [results, baselineValue] = await Promise.all([Promise.all(result), baseline]);
const known = results.length === 1
? this.cutKnownTypes(results[0], 'exports')
: this.cutKnownTypes(results, 'exports');
? await this.cutKnownTypes(results[0], 'exports')
: await this.cutKnownTypes(results, 'exports');
const oldBase = this.parse(baselineValue);
const newJsonValues = await this.jsonBaseliner.create(results);
const newJson = (isEmptyJsonSting(newJsonValues))
? '{}'
: newJsonValues;
let isEqual = true;
if (utils.normalizeEndings(newJsonValues) !== utils.normalizeEndings(oldBase.json)) isEqual = false;
if (utils.normalizeEndings(newJson) !== utils.normalizeEndings(oldBase.json)) isEqual = false;
if (!await compareKnownTypes(oldBase.knownEntities, known)) isEqual = false;

return {
Expand Down Expand Up @@ -107,15 +116,15 @@ export default class MDBaseliner extends AbstractBaseliner {
};
}

private cutKnownTypes = (parent: any, path: string, key?: string | number): IKnownType[] => {
private cutKnownTypes = async (parent: any, path: string, key?: string | number): Promise<IKnownType[]> => {
const obj = (key !== undefined)
? parent[key]
: parent;
if (isPrimitive(obj)) return [];
if (Array.isArray(obj)) {
return ([] as IKnownType[]).concat(
...obj.map((value, index) =>
this.cutKnownTypes(obj, `${path}[${index}]`, index)));
...await Promise.all(obj.map((value, index) =>
this.cutKnownTypes(obj, `${path}[${index}]`, index))));
}
const type = obj[dataTypes.image]
? dataTypes.image
Expand All @@ -127,16 +136,17 @@ export default class MDBaseliner extends AbstractBaseliner {
type,
name: path,
src: (type === dataTypes.html)
? htmlBeautify(obj.value)
? await htmlBeautify(obj.value)
: obj.value,
};
if (key !== undefined) delete parent[key];

return [result];
}

return ([] as IKnownType[]).concat(...Object.keys(obj)
.map(index => this.cutKnownTypes(obj, `${path}.${index}`, index)));
return ([] as IKnownType[]).concat(
...await Promise.all(Object.keys(obj)
.map(index => this.cutKnownTypes(obj, `${path}.${index}`, index))));
}
}

Expand All @@ -151,7 +161,9 @@ async function compareKnownTypes(oldBase: IKnownType[], newBase: IKnownType[]) {
if (value.type === dataTypes.image) {
if (!await compareImages(value.src, newValue.src)) return false;
} else {
if (utils.normalizeEndings(value.src) !== utils.normalizeEndings(newValue.src)) return false;
if (utils.normalizeEndings(value.src) !== utils.normalizeEndings(newValue.src)) {
return false;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/baset-baseliner-md/src/types/clean-html.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare const beautify: {
clean: any
};
export = beautify;
2 changes: 0 additions & 2 deletions packages/baset-baseliner-md/src/types/html-beautify.d.ts

This file was deleted.

0 comments on commit 0078675

Please sign in to comment.