@atjson/renderer-commonmark
Advanced tools
Comparing version 0.5.17 to 0.5.18
import { HIRNode } from '@atjson/hir'; | ||
import Renderer from '@atjson/renderer-hir'; | ||
export declare type Rule = (...args: any[]) => IterableIterator<string>; | ||
export interface AnnotationLookup { | ||
[key: string]: Rule; | ||
} | ||
import Renderer, { State } from '@atjson/renderer-hir'; | ||
export default class CommonmarkRenderer extends Renderer { | ||
annotationLookup: AnnotationLookup; | ||
constructor(annotationLookup?: AnnotationLookup); | ||
registerRule(type: string, rule: Rule): void; | ||
unregisterRule(type: string): void; | ||
willRender(): void; | ||
renderAnnotation(annotation: HIRNode): IterableIterator<any>; | ||
'root'(): IterableIterator<string>; | ||
'bold'(): IterableIterator<string>; | ||
'blockquote'(): IterableIterator<string>; | ||
'heading'(props: { | ||
level: number; | ||
}): IterableIterator<string>; | ||
'horizontal-rule'(): IterableIterator<string>; | ||
'image'(props: { | ||
alt: string; | ||
url: string; | ||
}): IterableIterator<string>; | ||
'italic'(): IterableIterator<string>; | ||
'line-break'(): IterableIterator<string>; | ||
'link'(props: { | ||
url: string; | ||
}): IterableIterator<string>; | ||
'list-item'(_: any, state: State): IterableIterator<string>; | ||
'ordered-list'(_: any, state: State): IterableIterator<string>; | ||
'unordered-list'(_: any, state: State): IterableIterator<string>; | ||
'paragraph'(): IterableIterator<string>; | ||
renderAnnotation(annotation: HIRNode, state: State): IterableIterator<any>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const renderer_hir_1 = require("@atjson/renderer-hir"); | ||
const MARKDOWN_RULES = { | ||
class CommonmarkRenderer extends renderer_hir_1.default { | ||
*'root'() { | ||
let document = yield; | ||
return document.join('').trimRight(); | ||
}, | ||
} | ||
*'bold'() { | ||
let text = yield; | ||
return `**${text.join('')}**`; | ||
}, | ||
} | ||
*'blockquote'() { | ||
@@ -21,3 +21,3 @@ let text = yield; | ||
return lines.slice(0, endOfQuote).map(line => `> ${line}`).concat(lines.slice(endOfQuote)).join('\n'); | ||
}, | ||
} | ||
*'heading'(props) { | ||
@@ -27,61 +27,71 @@ let heading = yield; | ||
return `${level} ${heading.join('')}\n\n`; | ||
}, | ||
} | ||
*'horizontal-rule'() { | ||
return '---\n\n'; | ||
}, | ||
} | ||
*'image'(props) { | ||
return `![${props.alt}](${props.url})`; | ||
}, | ||
} | ||
*'italic'() { | ||
let text = yield; | ||
return `*${text.join('')}*`; | ||
}, | ||
} | ||
*'line-break'() { | ||
return ' \n'; | ||
}, | ||
} | ||
*'link'(props) { | ||
let text = yield; | ||
return `[${text.join('')}](${props.url})`; | ||
}, | ||
*'list-item'() { | ||
let indent = ' '.repeat(this.indent); | ||
let item = yield; | ||
let indentedItem = item.join('').split('\n').map(line => indent + line).join('\n').trim(); | ||
if (this.type === 'ordered-list') { | ||
return `${indent}${this.index++}. ${indentedItem}`; | ||
} | ||
*'list-item'(_, state) { | ||
let indent = ' '.repeat(state.get('indent')); | ||
let rawItem = yield; | ||
let index = state.get('index'); | ||
let item = rawItem.join('').split('\n').map(line => indent + line).join('\n').trim(); | ||
if (state.get('type') === 'ordered-list') { | ||
item = `${indent}${index}. ${item}`; | ||
state.set('index', index + 1); | ||
} | ||
else if (this.type === 'unordered-list') { | ||
return `${indent}- ${indentedItem}`; | ||
else if (state.get('type') === 'unordered-list') { | ||
item = `${indent}- ${item}`; | ||
} | ||
return item; | ||
}, | ||
*'ordered-list'() { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup, | ||
} | ||
*'ordered-list'(_, state) { | ||
let indent = state.get('indent'); | ||
if (indent == null) { | ||
indent = -1; | ||
} | ||
state.push({ | ||
type: 'ordered-list', | ||
indent: (this.indent + 1) || 0, | ||
indent: indent + 1, | ||
index: 1 | ||
}); | ||
let list = yield; | ||
this.popScope(); | ||
state.pop(); | ||
let markdown = `${list.join('\n')}\n\n`; | ||
if (this.type === 'ordered-list' || this.type === 'unordered-list') { | ||
if (state.get('type') === 'ordered-list' || | ||
state.get('type') === 'unordered-list') { | ||
return `\n${markdown}`; | ||
} | ||
return markdown; | ||
}, | ||
*'unordered-list'() { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup, | ||
} | ||
*'unordered-list'(_, state) { | ||
let indent = state.get('indent'); | ||
if (indent == null) { | ||
indent = -1; | ||
} | ||
state.push({ | ||
type: 'unordered-list', | ||
indent: (this.indent + 1) || 0 | ||
indent: indent + 1, | ||
}); | ||
let list = yield; | ||
this.popScope(); | ||
state.pop(); | ||
let markdown = `${list.join('\n')}\n\n`; | ||
if (this.type === 'ordered-list' || this.type === 'unordered-list') { | ||
if (state.get('type') === 'ordered-list' || | ||
state.get('type') === 'unordered-list') { | ||
return `\n${markdown}`; | ||
} | ||
return markdown; | ||
}, | ||
} | ||
*'paragraph'() { | ||
@@ -95,23 +105,6 @@ let rawText = yield; | ||
} | ||
}; | ||
class CommonmarkRenderer extends renderer_hir_1.default { | ||
constructor(annotationLookup) { | ||
super(); | ||
this.annotationLookup = Object.assign(annotationLookup || {}, MARKDOWN_RULES); | ||
} | ||
registerRule(type, rule) { | ||
this.annotationLookup[type] = rule; | ||
} | ||
unregisterRule(type) { | ||
delete this.annotationLookup[type]; | ||
} | ||
willRender() { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup | ||
}); | ||
} | ||
*renderAnnotation(annotation) { | ||
let rule = this.annotationLookup[annotation.type]; | ||
*renderAnnotation(annotation, state) { | ||
let rule = this[annotation.type]; | ||
if (rule) { | ||
return yield* rule.call(this, annotation.attributes); | ||
return yield* this[annotation.type](annotation.attributes, state); | ||
} | ||
@@ -125,2 +118,2 @@ else { | ||
exports.default = CommonmarkRenderer; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFDQSx1REFBNEM7QUFRNUMsTUFBTSxjQUFj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FDLEVBQUUsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO1lBQzlELE1BQU0sQ0FBQyxJQUFJLENBQUM7UUFDZCxDQUFDO1FBQ0QsTUFBTSxDQUFDLEdBQUcsSUFBSSxNQUFNLENBQUM7SUFDdkIsQ0FBQztJQUVELENBQUMsZ0JBQWdCLENBQUMsVUFBbUIsRUFBRSxLQUFZO1FBQ2pELElBQUksSUFBSSxHQUFHLElBQUksQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLENBQUM7UUFDakMsRUFBRSxDQUFDLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQztZQUNULE1BQU0sQ0FBQyxLQUFLLENBQUMsQ0FBQyxJQUFJLENBQUMsVUFBVSxDQUFDLElBQUksQ0FBQyxDQUFDLFVBQVUsQ0FBQyxVQUFVLEVBQUUsS0FBSyxDQUFDLENBQUM7UUFDcEUsQ0FBQztRQUFDLElBQUksQ0FBQyxDQUFDO1lBQ04sSUFBSSxJQUFJLEdBQUcsS0FBSyxDQUFDO1lBQ2pCLE1BQU0sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFDO1FBQ3ZCLENBQUM7SUFDSCxDQUFDO0NBQ0Y7QUF6TEQscUNBeUxDIn0= |
import { HIRNode } from '@atjson/hir'; | ||
import Renderer from '@atjson/renderer-hir'; | ||
export declare type Rule = (...args: any[]) => IterableIterator<string>; | ||
export interface AnnotationLookup { | ||
[key: string]: Rule; | ||
} | ||
import Renderer, { State } from '@atjson/renderer-hir'; | ||
export default class CommonmarkRenderer extends Renderer { | ||
annotationLookup: AnnotationLookup; | ||
constructor(annotationLookup?: AnnotationLookup); | ||
registerRule(type: string, rule: Rule): void; | ||
unregisterRule(type: string): void; | ||
willRender(): void; | ||
renderAnnotation(annotation: HIRNode): IterableIterator<any>; | ||
'root'(): IterableIterator<string>; | ||
'bold'(): IterableIterator<string>; | ||
'blockquote'(): IterableIterator<string>; | ||
'heading'(props: { | ||
level: number; | ||
}): IterableIterator<string>; | ||
'horizontal-rule'(): IterableIterator<string>; | ||
'image'(props: { | ||
alt: string; | ||
url: string; | ||
}): IterableIterator<string>; | ||
'italic'(): IterableIterator<string>; | ||
'line-break'(): IterableIterator<string>; | ||
'link'(props: { | ||
url: string; | ||
}): IterableIterator<string>; | ||
'list-item'(_: any, state: State): IterableIterator<string>; | ||
'ordered-list'(_: any, state: State): IterableIterator<string>; | ||
'unordered-list'(_: any, state: State): IterableIterator<string>; | ||
'paragraph'(): IterableIterator<string>; | ||
renderAnnotation(annotation: HIRNode, state: State): IterableIterator<any>; | ||
} |
"use strict"; | ||
import Renderer from '@atjson/renderer-hir'; | ||
const MARKDOWN_RULES = { | ||
export default class CommonmarkRenderer extends Renderer { | ||
*'root'() { | ||
let document = yield; | ||
return document.join('').trimRight(); | ||
}, | ||
} | ||
*'bold'() { | ||
let text = yield; | ||
return `**${text.join('')}**`; | ||
}, | ||
} | ||
*'blockquote'() { | ||
@@ -20,3 +20,3 @@ let text = yield; | ||
return lines.slice(0, endOfQuote).map(line => `> ${line}`).concat(lines.slice(endOfQuote)).join('\n'); | ||
}, | ||
} | ||
*'heading'(props) { | ||
@@ -26,61 +26,71 @@ let heading = yield; | ||
return `${level} ${heading.join('')}\n\n`; | ||
}, | ||
} | ||
*'horizontal-rule'() { | ||
return '---\n\n'; | ||
}, | ||
} | ||
*'image'(props) { | ||
return `![${props.alt}](${props.url})`; | ||
}, | ||
} | ||
*'italic'() { | ||
let text = yield; | ||
return `*${text.join('')}*`; | ||
}, | ||
} | ||
*'line-break'() { | ||
return ' \n'; | ||
}, | ||
} | ||
*'link'(props) { | ||
let text = yield; | ||
return `[${text.join('')}](${props.url})`; | ||
}, | ||
*'list-item'() { | ||
let indent = ' '.repeat(this.indent); | ||
let item = yield; | ||
let indentedItem = item.join('').split('\n').map(line => indent + line).join('\n').trim(); | ||
if (this.type === 'ordered-list') { | ||
return `${indent}${this.index++}. ${indentedItem}`; | ||
} | ||
*'list-item'(_, state) { | ||
let indent = ' '.repeat(state.get('indent')); | ||
let rawItem = yield; | ||
let index = state.get('index'); | ||
let item = rawItem.join('').split('\n').map(line => indent + line).join('\n').trim(); | ||
if (state.get('type') === 'ordered-list') { | ||
item = `${indent}${index}. ${item}`; | ||
state.set('index', index + 1); | ||
} | ||
else if (this.type === 'unordered-list') { | ||
return `${indent}- ${indentedItem}`; | ||
else if (state.get('type') === 'unordered-list') { | ||
item = `${indent}- ${item}`; | ||
} | ||
return item; | ||
}, | ||
*'ordered-list'() { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup, | ||
} | ||
*'ordered-list'(_, state) { | ||
let indent = state.get('indent'); | ||
if (indent == null) { | ||
indent = -1; | ||
} | ||
state.push({ | ||
type: 'ordered-list', | ||
indent: (this.indent + 1) || 0, | ||
indent: indent + 1, | ||
index: 1 | ||
}); | ||
let list = yield; | ||
this.popScope(); | ||
state.pop(); | ||
let markdown = `${list.join('\n')}\n\n`; | ||
if (this.type === 'ordered-list' || this.type === 'unordered-list') { | ||
if (state.get('type') === 'ordered-list' || | ||
state.get('type') === 'unordered-list') { | ||
return `\n${markdown}`; | ||
} | ||
return markdown; | ||
}, | ||
*'unordered-list'() { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup, | ||
} | ||
*'unordered-list'(_, state) { | ||
let indent = state.get('indent'); | ||
if (indent == null) { | ||
indent = -1; | ||
} | ||
state.push({ | ||
type: 'unordered-list', | ||
indent: (this.indent + 1) || 0 | ||
indent: indent + 1, | ||
}); | ||
let list = yield; | ||
this.popScope(); | ||
state.pop(); | ||
let markdown = `${list.join('\n')}\n\n`; | ||
if (this.type === 'ordered-list' || this.type === 'unordered-list') { | ||
if (state.get('type') === 'ordered-list' || | ||
state.get('type') === 'unordered-list') { | ||
return `\n${markdown}`; | ||
} | ||
return markdown; | ||
}, | ||
} | ||
*'paragraph'() { | ||
@@ -94,23 +104,6 @@ let rawText = yield; | ||
} | ||
}; | ||
export default class CommonmarkRenderer extends Renderer { | ||
constructor(annotationLookup) { | ||
super(); | ||
this.annotationLookup = Object.assign(annotationLookup || {}, MARKDOWN_RULES); | ||
} | ||
registerRule(type, rule) { | ||
this.annotationLookup[type] = rule; | ||
} | ||
unregisterRule(type) { | ||
delete this.annotationLookup[type]; | ||
} | ||
willRender() { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup | ||
}); | ||
} | ||
*renderAnnotation(annotation) { | ||
let rule = this.annotationLookup[annotation.type]; | ||
*renderAnnotation(annotation, state) { | ||
let rule = this[annotation.type]; | ||
if (rule) { | ||
return yield* rule.call(this, annotation.attributes); | ||
return yield* this[annotation.type](annotation.attributes, state); | ||
} | ||
@@ -123,2 +116,2 @@ else { | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUNBLE9BQU8sUUFBUSxNQUFNLHNCQUFzQixDQUFDO0FBUTVDLE1BQU0sY0FBYyxHQUFxQjtJQU12QyxDQUFDLE1BQU07UUFDTCxJQUFJLFFBQVEsR0FBRyxLQUFLLENBQUM7UUFDckIsTUFBTSxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsRUFBRSxDQUFDLENBQUMsU0FBUyxFQUFFLENBQUM7SUFDdkMsQ0FBQztJQUtELENBQUMsTUFBTTtRQUNMLElBQUksSUFBSSxHQUFHLEtBQUssQ0FBQztRQUNqQixNQUFNLENBQUMsS0FBSyxJQUFJLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxJQUFJLENBQUM7SUFDaEMsQ0FBQztJQVFELENBQUMsWUFBWTtRQUNYLElBQUksSUFBSSxHQUFhLEtBQUssQ0FBQztRQUMzQixJQUFJLEtBQUssR0FBYSxJQUFJLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQ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BQUMsS0FBSyxDQUFDLENBQUMsSUFBSSxDQUFDLFVBQVUsQ0FBQyxJQUFJLENBQUMsQ0FBQyxVQUFVLENBQUMsVUFBVSxFQUFFLEtBQUssQ0FBQyxDQUFDO1FBQ3BFLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNOLElBQUksSUFBSSxHQUFHLEtBQUssQ0FBQztZQUNqQixNQUFNLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxFQUFFLENBQUMsQ0FBQztRQUN2QixDQUFDO0lBQ0gsQ0FBQztDQUNGIn0= |
{ | ||
"name": "@atjson/renderer-commonmark", | ||
"version": "0.5.17", | ||
"version": "0.5.18", | ||
"description": "A brand new TypeScript library.", | ||
@@ -24,4 +24,4 @@ "main": "dist/commonjs/index.js", | ||
"dependencies": { | ||
"@atjson/renderer-hir": "^0.5.17" | ||
"@atjson/renderer-hir": "^0.5.18" | ||
} | ||
} |
import { HIRNode } from '@atjson/hir'; | ||
import Renderer from '@atjson/renderer-hir'; | ||
import Renderer, { State } from '@atjson/renderer-hir'; | ||
export type Rule = (...args: any[]) => IterableIterator<string>; | ||
export default class CommonmarkRenderer extends Renderer { | ||
export interface AnnotationLookup { | ||
[key: string]: Rule; | ||
} | ||
const MARKDOWN_RULES: AnnotationLookup = { | ||
/** | ||
@@ -102,11 +97,13 @@ * The root allows us to normalize the document | ||
*/ | ||
*'list-item'(this: any): IterableIterator<string> { | ||
let indent: string = ' '.repeat(this.indent); | ||
let item: string[] = yield; | ||
let indentedItem: string = item.join('').split('\n').map(line => indent + line).join('\n').trim(); | ||
*'list-item'(_, state: State): IterableIterator<string> { | ||
let indent: string = ' '.repeat(state.get('indent')); | ||
let rawItem: string[] = yield; | ||
let index: number = state.get('index'); | ||
let item: string = rawItem.join('').split('\n').map(line => indent + line).join('\n').trim(); | ||
if (this.type === 'ordered-list') { | ||
return `${indent}${this.index++}. ${indentedItem}`; | ||
} else if (this.type === 'unordered-list') { | ||
return `${indent}- ${indentedItem}`; | ||
if (state.get('type') === 'ordered-list') { | ||
item = `${indent}${index}. ${item}`; | ||
state.set('index', index + 1); | ||
} else if (state.get('type') === 'unordered-list') { | ||
item = `${indent}- ${item}`; | ||
} | ||
@@ -122,14 +119,18 @@ | ||
*/ | ||
*'ordered-list'(this: any): IterableIterator<string> { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup, | ||
*'ordered-list'(_, state: State): IterableIterator<string> { | ||
let indent = state.get('indent'); | ||
if (indent == null) { | ||
indent = -1; | ||
} | ||
state.push({ | ||
type: 'ordered-list', | ||
indent: (this.indent + 1) || 0, | ||
indent: indent + 1, | ||
index: 1 | ||
}); | ||
let list = yield; | ||
this.popScope(); | ||
state.pop(); | ||
let markdown = `${list.join('\n')}\n\n`; | ||
if (this.type === 'ordered-list' || this.type === 'unordered-list') { | ||
if (state.get('type') === 'ordered-list' || | ||
state.get('type') === 'unordered-list') { | ||
return `\n${markdown}`; | ||
@@ -145,13 +146,18 @@ } | ||
*/ | ||
*'unordered-list'(this: any): IterableIterator<string> { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup, | ||
*'unordered-list'(_, state: State): IterableIterator<string> { | ||
let indent = state.get('indent'); | ||
if (indent == null) { | ||
indent = -1; | ||
} | ||
state.push({ | ||
type: 'unordered-list', | ||
indent: (this.indent + 1) || 0 | ||
indent: indent + 1, | ||
}); | ||
let list = yield; | ||
this.popScope(); | ||
state.pop(); | ||
let markdown = `${list.join('\n')}\n\n`; | ||
if (this.type === 'ordered-list' || this.type === 'unordered-list') { | ||
if (state.get('type') === 'ordered-list' || | ||
state.get('type') === 'unordered-list') { | ||
return `\n${markdown}`; | ||
@@ -176,30 +182,7 @@ } | ||
} | ||
}; | ||
export default class CommonmarkRenderer extends Renderer { | ||
annotationLookup: AnnotationLookup; | ||
constructor(annotationLookup?: AnnotationLookup) { | ||
super(); | ||
this.annotationLookup = Object.assign(annotationLookup || {}, MARKDOWN_RULES); | ||
} | ||
registerRule(type: string, rule: Rule) { | ||
this.annotationLookup[type] = rule; | ||
} | ||
unregisterRule(type: string) { | ||
delete this.annotationLookup[type]; | ||
} | ||
willRender() { | ||
this.pushScope({ | ||
annotationLookup: this.annotationLookup | ||
}); | ||
} | ||
*renderAnnotation(annotation: HIRNode) { | ||
let rule = this.annotationLookup[annotation.type]; | ||
*renderAnnotation(annotation: HIRNode, state: State) { | ||
let rule = this[annotation.type]; | ||
if (rule) { | ||
return yield* rule.call(this, annotation.attributes); | ||
return yield* this[annotation.type](annotation.attributes, state); | ||
} else { | ||
@@ -206,0 +189,0 @@ let text = yield; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25907
7
443
Updated@atjson/renderer-hir@^0.5.18