Skip to content

Commit

Permalink
Add jsdom for dom render
Browse files Browse the repository at this point in the history
  • Loading branch information
vn7n24fzkq committed Aug 16, 2020
1 parent c044f02 commit 6c01edc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"axios": "^0.19.2",
"d3": "^5.16.0",
"dotenv": "^8.2.0"
"dotenv": "^8.2.0",
"jsdom": "^16.4.0"
}
}
29 changes: 29 additions & 0 deletions src/templates/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const d3 = require("d3");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

class Card {
constructor(height = 1024, width = 1280) {
this.height = height;
this.width = width;
// use fake dom let us can get html element
const fakeDom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
this.body = d3.select(fakeDom.window.document).select("body");
this.svg = this.body
.append("div")
.attr("class", "container")
.append("svg")
.attr("width", width)
.attr("height", height);
}

getSVG() {
return this.svg;
}

toString() {
return this.body.select(".container").html();
}
}

module.exports = Card;
32 changes: 32 additions & 0 deletions src/templates/repo-top-lang-card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Theme = require("../const/theme");
const getRepoLanguage = require("../utils/github-api/langs");
const writeSVG = require("../utils/svg-writer");
const Card = require("./card");

function logMapElements(value, key, map) {}

function createLanguageNode() {}

const createRepoLanguageCard = async function (username) {
let langMap = await getRepoLanguage(username);
for (let [key, value] of langMap) {
console.log(key + " = " + value);
}

// Draw a line
let card= new Card();
console.log(card.toString());
let circle = card.getSVG()
.append("line")
.attr("x1", 5)
.attr("y1", 5)
.attr("x2", 500)
.attr("y2", 500)
.attr("stroke-width", 2)
.attr("stroke", "black");

// Output the result to console
console.log(card.toString());
};

module.exports = createRepoLanguageCard;

0 comments on commit 6c01edc

Please sign in to comment.