Skip to content

Instantly share code, notes, and snippets.

@fakeheal
Last active December 13, 2022 11:47
Show Gist options
  • Save fakeheal/8dc19c3bc0323f1f0c150ae25e3220cc to your computer and use it in GitHub Desktop.
Save fakeheal/8dc19c3bc0323f1f0c150ae25e3220cc to your computer and use it in GitHub Desktop.
Copy cards title & link as markdown from a specific Trello Board List
const print = (text, level = 'warn') => console[level](`HELPER: ${text}`)
const NOT_LABELED = 'Not Labeled';
const LIST_NAME = 'Live';
const SELECTORS = {
list: {
wrapper: 'list-wrapper',
header: 'list-header-name-assist'
},
card: {
wrapper: 'list-card',
label: '[data-testid="compact-card-label"]',
title: 'list-card-title'
}
}
let result = {};
const findListByName = (name) => {
const listNodes = document.getElementsByClassName(SELECTORS.list.header);
const listHeader = Array.from(listNodes).find(list => list?.innerText === name);
if (!listHeader) {
print(`List "${LIST_NAME}" was not found.`);
return null;
}
return findClassInParents(listHeader, SELECTORS.list.wrapper)
}
const findClassInParents = (node, className) => {
if (node.parentNode === document) {
return null;
}
if (node.parentNode.classList.contains(className)) {
return node.parentNode;
}
return findClassInParents(node.parentNode, className);
}
const listNode = findListByName(LIST_NAME);
if (listNode !== null) {
const cardNodes = listNode.getElementsByClassName(SELECTORS.card.wrapper);
const cards = Array.from(cardNodes);
if (cards.length === 0) {
print(`List ${LIST_NAME} didn't have any cards.`);
} else {
cards.forEach(card => {
const titleNode = card.getElementsByClassName(SELECTORS.card.title);
if (titleNode) {
const title = titleNode[0].innerText;
const item = `- [${title}](${card.href})`
const labelNodes = card.querySelectorAll(SELECTORS.card.label);
if (labelNodes.length > 0) {
const label = labelNodes[0].innerText;
if (!result[label]) {
result[label] = [];
}
result[label].push(item)
} else {
if (!result[NOT_LABELED]) {
result[NOT_LABELED] = [];
}
result[NOT_LABELED].push(item);
}
} else {
print('A card didn\'t have a title. Pitty...', 'error');
}
});
}
}
let markdown = ''
for (const key in result) {
markdown += `## ${key} \n`;
for (var i = 0; i < result[key].length; i++) {
markdown += `${result[key][i]} \n`;
}
}
console.log(`${markdown} \n\n\n`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment