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

feat: github readme parsing for templates #510

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,101 @@
import { Container } from '$lib/layout';
import { isSelfHosted } from '$lib/system';
import { connectTemplate } from '$lib/wizards/functions/cover.svelte';
import { afterUpdate, onMount } from 'svelte';
import { consoleVariables, isVcsEnabled } from '$routes/console/store';
import { template } from './store';

let html = '';
let container: HTMLDivElement;
onMount(async () => {
html = await fetchReadme();
});

afterUpdate(() => {
container.querySelectorAll('.anchor')?.forEach((e) => {
e.classList.add('u-hide');
});
container.querySelectorAll('h1').forEach((e) => {
e.classList.add('heading-level-4', 'common-section');
});
container.querySelectorAll('h2').forEach((e) => {
e.classList.add('heading-level-5', 'u-margin-block-start-16');
});
container.querySelectorAll('h3').forEach((e) => {
e.classList.add('heading-level-6', 'u-margin-block-start-16');
});
container.querySelectorAll('h4').forEach((e) => {
e.classList.add('heading-level-7', 'u-margin-block-start-8');
});
container.querySelectorAll('p').forEach((e) => {
e.classList.add('u-margin-block-start-8');
});
container.querySelectorAll('ul').forEach((e) => {
e.classList.add('u-margin-block-start-8', 'list');
});
container.querySelectorAll('li').forEach((e) => {
e.classList.add('list-item');
});
container.querySelectorAll('strong').forEach((e) => {
e.classList.add('u-bold');
});

container.querySelectorAll('.snippet-clipboard-content').forEach((e) => {
e.classList.add('box', 'u-margin-block-start-8', 'u-min-width-0');
});
container.querySelectorAll('.highlight').forEach((e) => {
e.classList.add('box', 'u-margin-block-start-8', 'u-min-width-0');
});
const table = container.querySelectorAll('table');

table.forEach((e) => {
e.classList.add('table');

const div1 = document.createElement('div');
const div2 = document.createElement('div');
div1.classList.add('table-with-scroll', 'u-margin-block-start-16');
div2.classList.add('table-wrapper');
e.parentNode.insertBefore(div1, e);
e.parentNode.insertBefore(div2, e.nextSibling);
div1.appendChild(div2);
div2.appendChild(e);
});

container.querySelectorAll('thead').forEach((e) => {
e.classList.add('table-thead');
});
container.querySelectorAll('tbody').forEach((e) => {
e.classList.add('table-tbody');
});
container.querySelectorAll('th').forEach((e) => {
e.classList.add('table-thead-col', 'eyebrow-heading-3');
e.style.cssText = '--p-col-width: 70;';
});
container.querySelectorAll('tr').forEach((e) => {
e.classList.add('table-row');
});
container.querySelectorAll('td').forEach((e) => {
e.classList.add('table-col');
e.style.cssText = '--p-col-width: 70;';
});
container.querySelectorAll('code:not(pre code)').forEach((e) => {
e.classList.add('inline-code');
});
});

async function fetchReadme() {
const runtime = $template.runtimes[0];
const url = `https://api.github.com/repos/${$template.providerOwner}/${$template.providerRepositoryId}/contents/${runtime.providerRootDirectory}/README.md`;

const response = await fetch(url, {
headers: {
Accept: 'application/vnd.github.html'
}
});

const readmeHTML = await response.text();
return readmeHTML;
}
</script>

<Container>
Expand Down Expand Up @@ -89,6 +182,12 @@
</Button>
</div>
</Card>
<Card>{@html $template.instructions}</Card>
<Card>
<div bind:this={container}>
{@html html}
</div>
</Card>
</section>
</div>
</Container>
Loading