UNPKG

ig-typedoc-theme

Version:

infragistics theme for typedoc API documentation with versioning and localization

97 lines (96 loc) 3.71 kB
import { readFileSync, existsSync } from 'fs'; import path from "path"; import { fileURLToPath } from "url"; import { DeclarationReflection, SignatureReflection, JSX, ReflectionKind } from "typedoc"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export function hasTypeParameters(reflection) { if (reflection instanceof DeclarationReflection || reflection instanceof SignatureReflection) { return reflection.typeParameters != null; } return false; } export function join(joiner, list, cb) { const result = []; for (const item of list) { if (result.length > 0) { result.push(joiner); } result.push(cb(item)); } return JSX.createElement(JSX.Fragment, null, result); } export function classNames(names) { return Object.entries(names) .filter(([, include]) => include) .map(([key]) => key) .join(" "); } export function renderTypeParametersSignature(typeParameters) { return (JSX.createElement(JSX.Fragment, null, !!typeParameters && typeParameters.length > 0 && (JSX.createElement(JSX.Fragment, null, JSX.createElement("span", { class: "tsd-signature-symbol" }, "<"), join(JSX.createElement("span", { class: "tsd-signature-symbol" }, ", "), typeParameters, (item) => (JSX.createElement("span", { class: "tsd-signature-type", "data-tsd-kind": ReflectionKind.singularString(item.kind) }, item.name))), JSX.createElement("span", { class: "tsd-signature-symbol" }, ">"))))); } export function renderFlags(flags) { return (JSX.createElement(JSX.Fragment, null, Array.from(flags).map((item) => (JSX.createElement(JSX.Fragment, null, JSX.createElement("span", { class: "tsd-flag ts-flag" + item }, item), " "))))); } /** * Insert word break tags ``<wbr>`` into the given string. * * Breaks the given string at ``_``, ``-`` and capital letters. * * @param str The string that should be split. * @return The original string containing ``<wbr>`` tags where possible. */ export function wbr(str) { const ret = []; const re = /[\s\S]*?(?:([^_-][_-])(?=[^_-])|([^A-Z])(?=[A-Z][^A-Z]))/g; let match; let i = 0; while ((match = re.exec(str))) { ret.push(match[0]); ret.push(JSX.createElement("wbr", null)); i += match[0].length; } ret.push(str.slice(i)); return ret; } export function stringify(data) { if (typeof data === "bigint") { return data.toString() + "n"; } return JSON.stringify(data); } export function getConfigData(context, prop, lang) { const configOption = context.options.getValue('config'); const product = context.options.getValue('product'); const prodArr = product.split('-'); const prodName = prodArr[prodArr.length - 1]; let fileName = prodName == 'angular' ? 'config.json' : prodName + '.config.json'; if (configOption && configOption !== "") { fileName = configOption; } const filePath = getConfigFilePath(fileName); const config = JSON.parse(readFileSync(filePath, 'utf8')); const settingOpt = context.options.getValue('localize'); const getLang = lang ? lang : settingOpt; let data; if (config && getLang && process.env.NODE_ENV) { data = config[getLang][process.env.NODE_ENV.trim()]; } const res = data ? data[prop] : ''; return res; } export function getConfigFilePath(fileName) { const normalizedPath = path.join(__dirname, '..', fileName); if (existsSync(normalizedPath)) { return normalizedPath; } else { // fallback to default config return path.join(__dirname, '..', 'config.json'); } }