Skip to content

Commit

Permalink
Merge pull request #171 from retejs/fix-api-markdown
Browse files Browse the repository at this point in the history
fix(typedoc,transformer): sanitize text
  • Loading branch information
Ni55aN authored Dec 26, 2024
2 parents 5e8ab8a + da8907d commit 0fbb2b4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions typedoc/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function getAnchor(data: ProjectReflection, id: number) {
}

function link(data, arg, text) {
return `[${text}](#${getAnchor(data, arg.target)})`;
return `[${sanitize(text)}](#${getAnchor(data, arg.target)})`;
}

function stringifyObject(type: ReflectionType) {
Expand All @@ -97,6 +97,15 @@ function getPriority(item: Reflection) {
return +(tags.find((item) => item.tag === '@priority')?.content[0].text || 0);
}

function sanitize(text: string) {
return text
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}

async function typedocToMarkdown(
data: ProjectReflection,
children: DeclarationReflection[] | undefined = [],
Expand All @@ -109,7 +118,7 @@ async function typedocToMarkdown(
if (parameters && parameters.length) {
markdown.push('| Parameter | Extends | Description |');
markdown.push('| ---- | ---- | ----------- |');
parameters.forEach(({ name, type, comment }) => markdown.push(`| ${name} | \`${type || 'any'}\` | ${comment} |`));
parameters.forEach(({ name, type, comment }) => markdown.push(`| ${name} | \`${sanitize(type) || 'any'}\` | ${comment} |`));
}
}
function throws(ref: Reflection) {
Expand Down

0 comments on commit 0fbb2b4

Please sign in to comment.