Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@atjson/renderer-hir

Package Overview
Dependencies
Maintainers
2
Versions
108
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@atjson/renderer-hir - npm Package Compare versions

Comparing version 0.5.20 to 0.7.0

68

dist/commonjs/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@atjson/core");
const document_1 = require("@atjson/document");
const hir_1 = require("@atjson/hir");

@@ -15,9 +15,20 @@ const escape = {

function escapeHTML(text) {
return text.replace(/[&<>"'`=]/g, function (chr) {
return escape[chr];
});
return text.replace(/[&<>"'`=]/g, (chr) => escape[chr]);
}
exports.escapeHTML = escapeHTML;
function compile(renderer, node, state) {
let generator = renderer.renderAnnotation(node, state);
function flatten(array) {
let flattenedArray = [];
for (let i = 0, len = array.length; i < len; i++) {
let item = array[i];
if (Array.isArray(item)) {
flattenedArray.push(...flatten(item));
}
else if (item != null) {
flattenedArray.push(item);
}
}
return flattenedArray;
}
function compile(renderer, node, state, schema) {
let generator = renderer.renderAnnotation(node, state, schema);
let result = generator.next();

@@ -27,11 +38,23 @@ if (result.done) {

}
return generator.next(node.children().map((childNode) => {
let children = node.children();
return generator.next(flatten(children.map((childNode, index) => {
state.set('previousAnnotation', children[index - 1]);
state.set('nextAnnotation', children[index + 1]);
if (childNode.type === 'text' && typeof childNode.text === 'string') {
return renderer.renderText(childNode.text);
return renderer.renderText(childNode.text, state);
}
else {
return compile(renderer, childNode, state);
return compile(renderer, childNode, state, schema);
}
})).value;
}))).value;
}
function get(object, key) {
if (key === '')
return object;
let [path, ...rest] = key.split('.');
if (object) {
return get(object[path], rest.join('.'));
}
return null;
}
class State {

@@ -48,7 +71,3 @@ constructor() {

get(key) {
let currentState = this._state[this._state.length - 1];
if (currentState) {
return currentState[key];
}
return null;
return get(this._state[this._state.length - 1], key);
}

@@ -67,13 +86,16 @@ set(key, value) {

class Renderer {
*renderAnnotation(annotation, state, schema) {
if (this[annotation.type]) {
return yield* this[annotation.type](annotation.attributes, state, schema);
}
return yield;
}
renderText(text) {
return text;
}
render(atjson) {
render(document) {
let annotationGraph;
if (atjson instanceof core_1.AtJSON) {
annotationGraph = new hir_1.HIR(atjson);
if (document instanceof document_1.default) {
annotationGraph = new hir_1.HIR(document);
}
else if (atjson instanceof hir_1.HIR) {
annotationGraph = atjson;
}
else {

@@ -83,3 +105,3 @@ throw new Error('Supplied arguments invalid.');

let state = new State();
let renderedDocument = compile(this, annotationGraph.rootNode, state);
let renderedDocument = compile(this, annotationGraph.rootNode, state, document.schema);
return renderedDocument;

@@ -89,2 +111,2 @@ }

exports.default = Renderer;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSx1Q0FBc0M7QUFDdEMscUNBQTJDO0FBRTNDLE1BQU0sTUFBTSxHQUFHO0lBQ2IsR0FBRyxFQUFFLE9BQU87SUFDWixHQUFHLEVBQUUsTUFBTTtJQUNYLEdBQUcsRUFBRSxNQUFNO0lBQ1gsR0FBRyxFQUFFLFFBQVE7SUFDYixHQUFHLEVBQUUsUUFBUTtJQUNiLEdBQUcsRUFBRSxRQUFRO0lBQ2IsR0FBRyxFQUFFLFFBQVE7Q0FDZCxDQUFDO0FBRUYsb0JBQTJCLElBQVk7SUFDckMsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsWUFBWSxFQUFFLFVBQVUsR0FBd0I7UUFDbEUsTUFBTSxDQUFDLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQztJQUNyQixDQUFDLENBQUMsQ0FBQztBQUNMLENBQUM7QUFKRCxnQ0FJQztBQUVELGlCQUFpQixRQUFrQixFQUFFLElBQWEsRUFBRS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xDQUFDLFFBQVEsRUFBRSxLQUFLLEVBQUUsUUFBUSxDQUFDLE1BQU0sQ0FBQyxDQUFDO1FBQzFGLE1BQU0sQ0FBQyxnQkFBZ0IsQ0FBQztJQUMxQixDQUFDO0NBQ0Y7QUF4QkQsMkJBd0JDIn0=
"use strict";
import { AtJSON } from '@atjson/core';
import Document from '@atjson/document';
import { HIR } from '@atjson/hir';

@@ -14,8 +14,19 @@ const escape = {

export function escapeHTML(text) {
return text.replace(/[&<>"'`=]/g, function (chr) {
return escape[chr];
});
return text.replace(/[&<>"'`=]/g, (chr) => escape[chr]);
}
function compile(renderer, node, state) {
let generator = renderer.renderAnnotation(node, state);
function flatten(array) {
let flattenedArray = [];
for (let i = 0, len = array.length; i < len; i++) {
let item = array[i];
if (Array.isArray(item)) {
flattenedArray.push(...flatten(item));
}
else if (item != null) {
flattenedArray.push(item);
}
}
return flattenedArray;
}
function compile(renderer, node, state, schema) {
let generator = renderer.renderAnnotation(node, state, schema);
let result = generator.next();

@@ -25,11 +36,23 @@ if (result.done) {

}
return generator.next(node.children().map((childNode) => {
let children = node.children();
return generator.next(flatten(children.map((childNode, index) => {
state.set('previousAnnotation', children[index - 1]);
state.set('nextAnnotation', children[index + 1]);
if (childNode.type === 'text' && typeof childNode.text === 'string') {
return renderer.renderText(childNode.text);
return renderer.renderText(childNode.text, state);
}
else {
return compile(renderer, childNode, state);
return compile(renderer, childNode, state, schema);
}
})).value;
}))).value;
}
function get(object, key) {
if (key === '')
return object;
let [path, ...rest] = key.split('.');
if (object) {
return get(object[path], rest.join('.'));
}
return null;
}
export class State {

@@ -46,7 +69,3 @@ constructor() {

get(key) {
let currentState = this._state[this._state.length - 1];
if (currentState) {
return currentState[key];
}
return null;
return get(this._state[this._state.length - 1], key);
}

@@ -64,13 +83,16 @@ set(key, value) {

export default class Renderer {
*renderAnnotation(annotation, state, schema) {
if (this[annotation.type]) {
return yield* this[annotation.type](annotation.attributes, state, schema);
}
return yield;
}
renderText(text) {
return text;
}
render(atjson) {
render(document) {
let annotationGraph;
if (atjson instanceof AtJSON) {
annotationGraph = new HIR(atjson);
if (document instanceof Document) {
annotationGraph = new HIR(document);
}
else if (atjson instanceof HIR) {
annotationGraph = atjson;
}
else {

@@ -80,6 +102,6 @@ throw new Error('Supplied arguments invalid.');

let state = new State();
let renderedDocument = compile(this, annotationGraph.rootNode, state);
let renderedDocument = compile(this, annotationGraph.rootNode, state, document.schema);
return renderedDocument;
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE9BQU8sRUFBRSxNQUFNLEVBQUUsTUFBTSxjQUFjLENBQUM7QUFDdEMsT0FBTyxFQUFFLEdBQUcsRUFBVyxNQUFNLGFBQWEsQ0FBQztBQUUzQyxNQUFNLE1BQU0sR0FBRztJQUNiLEdBQUcsRUFBRSxPQUFPO0lBQ1osR0FBRyxFQUFFLE1BQU07SUFDWCxHQUFHLEVBQUUsTUFBTTtJQUNYLEdBQUcsRUFBRSxRQUFRO0lBQ2IsR0FBRyxFQUFFLFFBQVE7SUFDYixHQUFHLEVBQUUsUUFBUTtJQUNiLEdBQUcsRUFBRSxRQUFRO0NBQ2QsQ0FBQztBQUVGLE1BQU0scUJBQXFCLElBQVk7SUFDckMsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsWUFBWSxFQUFFLFVBQVUsR0FBd0I7UUFDbEUsTUFBTSxDQUFDLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQztJQUNyQixDQUFDLENBQUMsQ0FBQztBQUNMLENBQUM7QUFFRCxpQkFBaUIsUUFBa0IsRUFBRSxJQUFhLEVBQUUsS0FBWTtJQUM5RCxJQUFJLFNBQVMsR0FBRyxRQUFRLENBQUMsZ0JBQWdCLENBQUMsSUFB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UsUUFBUSxDQUFDLE1BQU0sQ0FBQyxDQUFDO1FBQzFGLE1BQU0sQ0FBQyxnQkFBZ0IsQ0FBQztJQUMxQixDQUFDO0NBQ0YifQ==
{
"name": "@atjson/renderer-hir",
"version": "0.5.20",
"version": "0.7.0",
"description": "A brand new TypeScript library.",

@@ -18,9 +18,9 @@ "main": "dist/commonjs/index.js",

"devDependencies": {
"tslint": "^5.7.0",
"typescript": "^2.4.2"
"tslint": "^5.9.1",
"typescript": "^2.6.2"
},
"dependencies": {
"@atjson/core": "^0.5.12",
"@atjson/hir": "^0.5.17"
"@atjson/document": "^0.7.0",
"@atjson/hir": "^0.7.0"
}
}

@@ -1,2 +0,2 @@

import { AtJSON } from '@atjson/core';
import Document, { Schema } from '@atjson/document';
import { HIR, HIRNode } from '@atjson/hir';

@@ -15,9 +15,20 @@

export function escapeHTML(text: string): string {
return text.replace(/[&<>"'`=]/g, function (chr: keyof typeof escape) {
return escape[chr];
});
return text.replace(/[&<>"'`=]/g, (chr: keyof typeof escape) => escape[chr]);
}
function compile(renderer: Renderer, node: HIRNode, state: State): any {
let generator = renderer.renderAnnotation(node, state);
function flatten(array) {
let flattenedArray = [];
for (let i = 0, len = array.length; i < len; i++) {
let item = array[i];
if (Array.isArray(item)) {
flattenedArray.push(...flatten(item));
} else if (item != null) {
flattenedArray.push(item);
}
}
return flattenedArray;
}
function compile(renderer: Renderer, node: HIRNode, state: State, schema: Schema) {
let generator = renderer.renderAnnotation(node, state, schema);
let result = generator.next();

@@ -28,15 +39,28 @@ if (result.done) {

return generator.next(node.children().map((childNode: HIRNode) => {
let children = node.children();
return generator.next(flatten(children.map((childNode: HIRNode, index: number) => {
state.set('previousAnnotation', children[index - 1]);
state.set('nextAnnotation', children[index + 1]);
if (childNode.type === 'text' && typeof childNode.text === 'string') {
return renderer.renderText(childNode.text);
return renderer.renderText(childNode.text, state);
} else {
return compile(renderer, childNode, state);
return compile<T>(renderer, childNode, state, schema);
}
})).value;
}))).value;
}
interface StateList {
[key: string]: value: any;
[key: string]: any;
}
function get(object: any, key: string): any {
if (key === '') return object;
let [path, ...rest] = key.split('.');
if (object) {
return get(object[path], rest.join('.'));
}
return null;
}
export class State {

@@ -58,7 +82,3 @@ private _state: StateList[];

get(key: string): any {
let currentState: StateList | null = this._state[this._state.length - 1];
if (currentState) {
return currentState[key];
}
return null;
return get(this._state[this._state.length - 1], key);
}

@@ -76,15 +96,18 @@

export default abstract class Renderer {
abstract renderAnnotation(node: HIRNode, state: State): IterableIterator<any>;
export default class Renderer {
*renderAnnotation(annotation: HIRNode, state: State, schema: Schema) {
if (this[annotation.type]) {
return yield* this[annotation.type](annotation.attributes, state, schema);
}
return yield;
}
renderText(text: string): string {
return text;
},
}
render(atjson: AtJSON | HIR): any {
render(document: Document): T {
let annotationGraph;
if (atjson instanceof AtJSON) {
annotationGraph = new HIR(atjson);
} else if (atjson instanceof HIR) {
annotationGraph = atjson;
if (document instanceof Document) {
annotationGraph = new HIR(document);
} else {

@@ -95,5 +118,5 @@ throw new Error('Supplied arguments invalid.');

let state = new State();
let renderedDocument = compile(this, annotationGraph.rootNode, state);
let renderedDocument = compile<T>(this, annotationGraph.rootNode, state, document.schema);
return renderedDocument;
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc