graphqldoc
Version:
Generate GraphQL docs from schema.
85 lines (84 loc) • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const introspection_1 = require("./introspection");
class HTML {
constructor() {
this.index = 1;
}
code(code) {
return `<code class="highlight"><table class="code"><tbody>${code}</tbody></table></code>`;
}
highlight(text) {
return `<strong>${text}</strong>`;
}
sup(text) {
return ` <sup>${text}</sup>`;
}
line(code) {
const row = this.index++;
return `<tr class="row"><td id="L${row}" class="td-index">${row}</td><td id="LC${row}" class="td-code">${code || ''}</td></tr>`;
}
tab(code) {
return `<span class="tab">${code}</span>`;
}
keyword(keyword) {
return `<span class="keyword operator ts">${keyword}</span>`;
}
comment(comment) {
return `<span class="comment line"># ${comment}</span>`;
}
identifier(type) {
return `<span class="identifier">${type.name}</span>`;
}
parameter(arg) {
return `<span class="variable parameter">${arg.name}</span>`;
}
property(name) {
return `<span class="meta">${name}</span>`;
}
useIdentifier(type, toUrl) {
switch (type.kind) {
case introspection_1.LIST:
return '[' + this.useIdentifier(type.ofType, toUrl) + ']';
case introspection_1.NON_NULL:
return this.useIdentifier(type.ofType, toUrl) + '!';
default:
return `<a class="support type" href="${toUrl}">${type.name}</a>`;
}
}
useIdentifierLength(type, base = 0) {
switch (type.kind) {
case introspection_1.LIST:
return this.useIdentifierLength(type.ofType, base + 2);
case introspection_1.NON_NULL:
return this.useIdentifierLength(type.ofType, base + 1);
default:
return base + type.name.length;
}
}
value(val) {
return val[0] === '"' ?
`<span class="string">${val}</span>` :
`<span class="constant numeric">${val}</span>`;
}
}
exports.HTML = HTML;
function split(text, len) {
return text
.split(/\s+/)
.reduce((result, word) => {
let last = result.length - 1;
let lineLen = result[last].length;
if (lineLen === 0) {
result[last] = word;
}
else if (lineLen < len) {
result[last] = result[last] + ' ' + word;
}
else {
result.push(word);
}
return result;
}, ['']);
}
exports.split = split;