fix: fix errors and reformat code

This commit is contained in:
saicaca
2024-08-03 16:40:20 +08:00
parent 1f93499ece
commit 0ad144add3
44 changed files with 550 additions and 558 deletions

View File

@@ -10,72 +10,53 @@ import { h } from 'hastscript'
* @returns {import('mdast').Parent} The created GitHub Card component.
*/
export function GithubCardComponent(properties, children) {
if (Array.isArray(children) && children.length !== 0)
return h("div",
{ class: 'hidden' },
['Invalid directive. ("github" directive must be leaf type "::github{repo="owner/repo"}")']
);
if (Array.isArray(children) && children.length !== 0)
return h('div', { class: 'hidden' }, [
'Invalid directive. ("github" directive must be leaf type "::github{repo="owner/repo"}")',
])
if (!properties.repo || !properties.repo.includes("/"))
return h("div",
{ class: 'hidden' },
'Invalid repository. ("repo" attributte must be in the format "owner/repo")'
);
if (!properties.repo || !properties.repo.includes('/'))
return h(
'div',
{ class: 'hidden' },
'Invalid repository. ("repo" attributte must be in the format "owner/repo")',
)
const repo = properties.repo;
const repo = properties.repo
const cardUuid = `GC${Math.random().toString(36).slice(-6)}` // Collisions are not important
const nAvatar = h(
`div#${cardUuid}-avatar`,
{ class: "gc-avatar"},
)
const nAvatar = h(`div#${cardUuid}-avatar`, { class: 'gc-avatar' })
const nLanguage = h(
`span#${cardUuid}-language`,
{ class: "gc-language" },
"Waiting..."
{ class: 'gc-language' },
'Waiting...',
)
const nTitle = h(
`div`,
{ class: "gc-titlebar" },
[
h("div", { class: "gc-titlebar-left"}, [
h("div", { class: "gc-owner" }, [
nAvatar,
h("div", { class: "gc-user" }, repo.split("/")[0] ),
]),
h("div", { class: "gc-divider" }, "/" ),
h("div", { class: "gc-repo" }, repo.split("/")[1] )
const nTitle = h(`div`, { class: 'gc-titlebar' }, [
h('div', { class: 'gc-titlebar-left' }, [
h('div', { class: 'gc-owner' }, [
nAvatar,
h('div', { class: 'gc-user' }, repo.split('/')[0]),
]),
h("div", { class: "github-logo"})
]
)
h('div', { class: 'gc-divider' }, '/'),
h('div', { class: 'gc-repo' }, repo.split('/')[1]),
]),
h('div', { class: 'github-logo' }),
])
const nDescription = h(
`div#${cardUuid}-description`,
{ class: "gc-description" },
"Waiting for api.github.com..."
{ class: 'gc-description' },
'Waiting for api.github.com...',
)
const nStars = h(
`div#${cardUuid}-stars`,
{ class: "gc-stars" },
"00K"
)
const nForks = h(
`div#${cardUuid}-forks`,
{ class: "gc-forks" },
"0K"
)
const nLicense = h(
`div#${cardUuid}-license`,
{ class: "gc-license" },
"0K"
)
const nStars = h(`div#${cardUuid}-stars`, { class: 'gc-stars' }, '00K')
const nForks = h(`div#${cardUuid}-forks`, { class: 'gc-forks' }, '0K')
const nLicense = h(`div#${cardUuid}-license`, { class: 'gc-license' }, '0K')
const nScript = h(
`script#${cardUuid}-script`,
{ type: "text/javascript", defer: true },
{ type: 'text/javascript', defer: true },
`
fetch('https://api.github.com/repos/${repo}', { referrerPolicy: "no-referrer" }).then(response => response.json()).then(data => {
document.getElementById('${cardUuid}-description').innerText = data.description.replace(/:[a-zA-Z0-9_]+:/g, '');
@@ -97,22 +78,22 @@ export function GithubCardComponent(properties, children) {
c.classList.add("fetch-error");
console.warn("[GITHUB-CARD] (Error) Loading card for ${repo} | ${cardUuid}.")
})
`
`,
)
return h(`a#${cardUuid}-card`,
{ class: "card-github fetch-waiting no-styling",
return h(
`a#${cardUuid}-card`,
{
class: 'card-github fetch-waiting no-styling',
href: `https://github.com/${repo}`,
target: '_blank',
repo },
repo,
},
[
nTitle,
nDescription,
h("div",
{ class: "gc-infobar" },
[nStars, nForks, nLicense, nLanguage]
),
nScript
]
);
}
h('div', { class: 'gc-infobar' }, [nStars, nForks, nLicense, nLanguage]),
nScript,
],
)
}