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
7 changes: 0 additions & 7 deletions assets/data/cards_pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,6 @@
"Back-end"
]
},
{
"title": "Python",
"description": "O Python é uma linguagem de programação amplamente usada em aplicações da Web, desenvolvimento de software, ciência de dados e machine learning (ML). Os desenvolvedores usam o Python porque é eficiente e fácil de aprender e pode ser executada em muitas plataformas diferentes.",
"tags": [
"Back-end"
]
},
{
"title": "UI Design",
"description": "É o desenho e execução de uma interface para a pessoa usuária. Por exemplo, garantir que um botão vai ser visto e apertado.",
Expand Down
57 changes: 40 additions & 17 deletions assets/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@ function searchCards() {

}

function formatCardTitle(title) {
let formatedWord = title.replace(/\s+/g, "-").toLowerCase();
formatedWord = formatedWord
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");
formatedWord = formatedWord.replace(/[^\w\-]+/g, "");
return formatedWord;
}

function insertCardsIntoHtml(data) {
let cards = `<div class="msg">
<div class=collumn-1>
Expand All @@ -94,17 +85,17 @@ function insertCardsIntoHtml(data) {
</div>
</div>`
data.forEach((card) => {
const formatedTitle = formatCardTitle(card.title);
const cardId = generateCardId(card.id, card.title, card.description)
cards += `
<section class="card" tags="${
card.tags ? card.tags : "Todos"
}" id="${formatedTitle}">
}" id="${cardId}">
<div class="card__header">
<h3 class="card__title">${card.title}</h3>
<img
alt="star"
unique-title="${formatedTitle}"
id="fav_${formatedTitle}"
unique-title="${cardId}"
id="fav_${cardId}"
src="${
card.tags.includes("Favoritos")
? starIconFilled
Expand Down Expand Up @@ -137,7 +128,7 @@ function insertCardsIntoHtml(data) {
}

function addFavoriteTagToCard(cardId) {
const card = document.querySelector(`#${cardId}`);
const card = document.getElementById(cardId);
const tags = card.getAttribute("tags").split(",");

if (tags.includes("Favoritos")) {
Expand All @@ -149,7 +140,7 @@ function addFavoriteTagToCard(cardId) {
card.setAttribute("tags", tags);
}

function setCardAsFavorite(cardId, favId) {
function setCardAsFavorite(cardId) {
const favIcon = document.querySelector(`#fav_${cardId}`);

if (favoriteCards.includes(cardId)) {
Expand All @@ -174,8 +165,8 @@ async function loadFavoriteCardsId() {

async function addFavoriteTag(cards) {
cards.map((card) => {
const formatedTitle = formatCardTitle(card.title);
if (favoriteCards.includes(formatedTitle)) {
const cardId = generateCardId(card.id, card.title, card.description)
if (favoriteCards.includes(cardId)) {
if (!card.tags) {
card.tags = [];
}
Expand Down Expand Up @@ -206,3 +197,35 @@ async function getCardsFromJson() {
searchInput.addEventListener("input", searchCards);
filterSelect.addEventListener("change", filterCards);
getCardsFromJson();

/**
* Generates a card ID using a default UUID or a hash of the card description.
*
* @param {string} defaultCardId - A default UUID generated by the CLI.
* @param {string} title - The title of the card.
* @param {string} description - The description of the card.
* @returns {string} - A generated ID
*/
function generateCardId(defaultCardId, title, description) {
if (defaultCardId) return defaultCardId;
return generateContentId(title, description);
}

/**
* Calculates a simple hash of the given content.
*
* @param {string} content - The content to be hashed.
* @param {string} title - An additional title to be added to the content.
* @param {number} hash - The initial hash value.
* @returns {string} The hashed representation of the content.
*/
function generateContentId(title = '', description = '', hash = 5381) {
const data = (title + description).slice(0, 32).split(' ').join('')

for (let i = 0; i < data.length; i++) {
hash = ((hash << 5) + hash) + data.charCodeAt(i);
}

const hashString = Math.abs(hash).toString(36); // Convert to base-36 string
return hashString;
}