Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emit markdown for descriptions #137

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 8 additions & 93 deletions lib/markup.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { marked } from 'marked';
import hljs from 'highlight.js';
import cheerio from 'cheerio';

marked.setOptions({
highlight: code => hljs.highlightAuto(code).value,
});

export default function (doc) {
console.log('transforming markup for document');
for (const data of doc.data) {
const { id, attributes } = data;
console.log(`\tGenerating markup for ${id}`);

try {
const description = attributes.description;

if (description) {
attributes.description = highlight(description);
attributes.description = fixFilename(description);
}

// console.log('\tcompleted highlighting')

replaceDescriptionFor(attributes.methods);
replaceDescriptionFor(attributes.properties);
replaceDescriptionFor(attributes.events);
// console.log(`\tMarkup Generated for ${id}\n`)
} catch (e) {
console.log(`Error generating markup for ${id}`);
console.log(e);
process.exit(1);
}
}

console.log('completed markup transformation for document');
return doc;
}

Expand All @@ -41,90 +27,19 @@ function replaceDescriptionFor(items) {
items.forEach(item => {
let itemDescription = item.description;
if (itemDescription) {
item.description = highlight(itemDescription);
item.description = fixFilename(itemDescription);
}
});
}
}

function highlight(description) {
export function fixFilename(description) {
if (description) {
description = description.replace(/&#(\d+);/g, function (match, dec) {
return String.fromCharCode(dec);
});
}
let markedup = removeHLJSPrefix(marked.parse(description));
let $ = cheerio.load(markedup);

let codeBlocks = $('pre code');

codeBlocks.each((i, el) => {
let element = $(el);
let klass = element.attr('class');
let lang = '';
let tableHeader = '';
if (klass) {
let type = klass.split('-').pop();
if (isFile(type)) {
tableHeader = `
<thead>
<tr>
<td colspan="2">${type}</td>
</tr>
</thead>`;
}
lang = determineLanguage(type);
}
let lines = element.html().split('\n');

// get rid of empty blank line
if (lines[lines.length - 1].trim() === '') {
lines.pop();
}

let wrappedLines = `<pre>${lines.join('\n')}</pre>`;
let lineNumbers = lines.map((_, i) => `${i + 1}\n`).join('');

element.parent().after(`<div class="highlight ${lang}">
<div class="ribbon"></div>
<div class="scroller">
<table class="CodeRay">${tableHeader}
<tbody>
<tr>
<td class="line-numbers"><pre>${lineNumbers}</pre></td>
<td class="code">${wrappedLines}</td>
</tr>
</tbody>
</table>
</div>
</div>
`);

element.parent().remove();
});

return $.html();
}

function determineLanguage(maybeFileName) {
const lang = maybeFileName.split('.').pop();
switch (lang) {
case 'js':
case 'javascript':
return 'javascript';
case 'ts':
return 'typescript';
case 'hbs':
return 'handlebars';
default:
return lang;
description = description
.replaceAll(/```([^\n]+)\.(js|hbs|ts)\n/g, '```$2 {data-filename=$1.$2}\n')
.replaceAll('```hbs', '```handlebars')
.replaceAll('```no-highlight', '```');
}
}

function removeHLJSPrefix(string) {
return string.replace(/hljs-/g, '');
}

function isFile(string) {
return /\./.test(string);
return description;
}
Loading