Skip to content
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
18 changes: 0 additions & 18 deletions .eslintrc

This file was deleted.

27 changes: 27 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "https://biomejs.dev/schemas/2.4.15/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignoreUnknown": true,
"includes": ["**", "!.claude"]
},
"formatter": {
"enabled": true,
"indentStyle": "tab"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
}
}
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dist"
],
"scripts": {
"lint": "eslint .",
"lint": "biome check .",
"format": "biome check --write .",
"build": "rollup -c",
"build-watch": "npm run build --watch",
"prebuild": "del-cli dist && npm run lint",
Expand Down Expand Up @@ -37,10 +38,8 @@
},
"homepage": "https://github.com/RocktimSaikia/github-card#readme",
"devDependencies": {
"@biomejs/biome": "2.4.15",
"del-cli": "^3.0.1",
"eslint": "^7.24.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-import": "2.22.1",
"rollup": "^2.45.2",
"rollup-plugin-uglify": "^6.0.4"
}
Expand Down
16 changes: 8 additions & 8 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { uglify } from 'rollup-plugin-uglify';
import pkg from './package.json';

export default [
{
input: 'src/index.js',
output: {
file: pkg.module,
format: 'es',
},
plugins: [uglify()],
},
{
input: 'src/index.js',
output: {
file: pkg.module,
format: 'es',
},
plugins: [uglify()],
},
];
12 changes: 6 additions & 6 deletions src/escape.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Escape user-controlled values (e.g. GitHub name/bio) before injecting them
// into the card's innerHTML, to prevent HTML/attribute injection.
function escapeHtml(value) {
return String(value)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
return String(value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}

export default escapeHtml;
23 changes: 11 additions & 12 deletions src/format.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/* eslint-disable no-restricted-properties */
function nFormatter(value) {
if (!(value >= 1000)) return value;
let newValue = value;
const suffixes = ['', 'K', 'M', 'B', 'T'];
let suffixNum = 0;
while (newValue >= 1000) {
newValue /= 1000;
suffixNum += 1;
}
if (!(value >= 1000)) return value;
let newValue = value;
const suffixes = ['', 'K', 'M', 'B', 'T'];
let suffixNum = 0;
while (newValue >= 1000) {
newValue /= 1000;
suffixNum += 1;
}

newValue = newValue.toPrecision(3);
newValue = newValue.toPrecision(3);

newValue += suffixes[suffixNum];
return newValue;
newValue += suffixes[suffixNum];
return newValue;
}

export default nFormatter;
93 changes: 46 additions & 47 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint no-undef: 0 */
import widgetStyle from './style.js';
import nFormatter from './format.js';
import escapeHtml from './escape.js';
import nFormatter from './format.js';
import widgetStyle from './style.js';

const template = document.createElement('template');

Expand All @@ -13,56 +12,56 @@ template.innerHTML = `
`;

class myCard extends HTMLElement {
constructor() {
super();
constructor() {
super();

this._shadowRoot = this.attachShadow({ mode: 'open' });
this._shadowRoot.append(template.content.cloneNode(true));
}
this._shadowRoot = this.attachShadow({ mode: 'open' });
this._shadowRoot.append(template.content.cloneNode(true));
}

static get observedAttributes() {
return ['data-theme'];
}
static get observedAttributes() {
return ['data-theme'];
}

attributeChangedCallback(attr, oldValue, newValue) {
if (attr === 'data-theme' && oldValue !== newValue) {
this.setTheme(newValue);
}
}
attributeChangedCallback(attr, oldValue, newValue) {
if (attr === 'data-theme' && oldValue !== newValue) {
this.setTheme(newValue);
}
}

connectedCallback() {
this.fetchData(this.getAttribute('data-user')).then((response) => {
const cardTemplate = this.createCard(response);
this._shadowRoot.querySelector('.card').innerHTML = cardTemplate;
});
}
connectedCallback() {
this.fetchData(this.getAttribute('data-user')).then((response) => {
const cardTemplate = this.createCard(response);
this._shadowRoot.querySelector('.card').innerHTML = cardTemplate;
});
}

setTheme(theme) {
switch (theme) {
case 'dark':
this._shadowRoot.querySelector('.card').classList.add('dark');
break;
default:
this._shadowRoot.querySelector('.card').classList.remove('dark');
break;
}
}
setTheme(theme) {
switch (theme) {
case 'dark':
this._shadowRoot.querySelector('.card').classList.add('dark');
break;
default:
this._shadowRoot.querySelector('.card').classList.remove('dark');
break;
}
}

/** Fetch the data */
async fetchData(response) {
const data = await fetch(`https://api.github.com/users/${response}`, {
method: 'GET',
});
const responseult = await data.json();
return responseult;
}
/** Fetch the data */
async fetchData(response) {
const data = await fetch(`https://api.github.com/users/${response}`, {
method: 'GET',
});
const responseult = await data.json();
return responseult;
}

createCard(user) {
const login = escapeHtml(user.login);
const htmlUrl = escapeHtml(user.html_url);
const name = escapeHtml(user.name);
const bio = escapeHtml(user.bio ?? '');
return `
createCard(user) {
const login = escapeHtml(user.login);
const htmlUrl = escapeHtml(user.html_url);
const name = escapeHtml(user.name);
const bio = escapeHtml(user.bio ?? '');
return `
<div class="cover"></div>
<div class="card-wrapper">
<a href="https://github.com/${login}" target="_blank" rel="noopener"><img id="github-logo" src="https://i.ibb.co/frv5pB3/github-logo.png" alt="github-logo" border="0"></a>
Expand Down Expand Up @@ -90,7 +89,7 @@ class myCard extends HTMLElement {
</div>
</div>
`;
}
}
}

customElements.define('github-card', myCard);
Loading