Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Port to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
vHanda committed Sep 10, 2018
1 parent c80a8c3 commit 5d345d9
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 214 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
3 changes: 3 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)


This is a tool which converts the Google Keep archive to markdown documents with YAML front matter. The Google Keep archive can be obtained from the Google Takeout page.

https://www.google.com/settings/takeout/custom/keep
39 changes: 0 additions & 39 deletions main.js

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "This is a tool which converts the Google Keep archive to markdown documents with YAML front matter. The Google Keep archive can be obtained from the Google Takeout page.",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha -u tdd ./"
"test": "./node_modules/.bin/mocha -u tdd ./dist/*.test.js"
},
"author": "",
"dependencies": {
Expand All @@ -16,6 +16,9 @@
"uuid": "^3.0.1"
},
"devDependencies": {
"chai": "^3.5.0"
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.4",
"chai": "^3.5.0",
"typescript": "^3.0.3"
}
}
70 changes: 0 additions & 70 deletions parser.js

This file was deleted.

24 changes: 0 additions & 24 deletions parser.test.js

This file was deleted.

79 changes: 0 additions & 79 deletions serializer.js

This file was deleted.

39 changes: 39 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

var fs = require("fs");
var parser = require("./parser.js");
var serializer = require("./serializer.js");

// TODO: parse argv and get directory file list
// parse output
//

// Take notes and then convert it into markdown
//
// .content
// -> This needs to be converted to markdown
// -> listitem
//

if (process.argv.length != 4) {
console.log("Usage main.js inputDir outputDir");
process.exit(1);
}

var inputDir = process.argv[2];
var outputDir = process.argv[3];

var convert = function(filePath, outputDir) {
var data = fs.readFileSync(filePath);
var note = parser(data);
var output = serializer.serialize(note);

output.forEach(d => {
console.log(filePath, d[0]);
fs.writeFileSync(outputDir + "/" + d[0], d[1]);
});
};

var files = fs.readdirSync(inputDir);
files = files.filter(t => t.endsWith(".html"));
files.forEach(filePath => convert(inputDir + "/" + filePath, outputDir));
27 changes: 27 additions & 0 deletions src/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var assert = require("chai").assert;
import { parse } from "./parser";

suite("Parser", function() {
test("Should parse some basic info", function() {
var data = `
<html><body><div class="note DEFAULT"><div class="heading">
21 Jun 2016, 22:39:47</div>
<div class="title">Ll</div>
<div class="content">Hearts of darkness<br>Water ship down<br>The Dubliners<br><br></div>
<div class="labels"><span class="label">Reading List</span><span class="label">Another Tag</span></div>
</div></body></html>
`;

console.log(parse);
var note = parse(data);
assert.deepEqual(note.title, "Ll");
// FIXME: Is the extra space really required?
assert.deepEqual(
note.content,
"Hearts of darkness \nWater ship down \nThe Dubliners"
);
assert.deepEqual(note.tags, ["Reading List", "Another Tag"]);
assert.notOk(note.archived);
assert.deepEqual(note.date, "2016-06-21T20:39:47.000Z");
});
});
78 changes: 78 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var cheerio = require("cheerio");
var toMarkdown = require("to-markdown");
var moment = require("moment");

function getImages(node) {
var images = [];
if (node instanceof Array) {
node.forEach(child => {
images = images.concat(getImages(child));
});
return images;
}

if (node.name == "img") {
var img = node.attribs.src;
images.push(img);
return images;
}

if (!node.children) return [];

node.children.forEach(child => {
images = images.concat(getImages(child));
});
return images;
}

var converter = {
filter: "div",
/*
filter: function(node) {
return node.className.indexOf('listitem') != -1;
},
*/
replacement: function(innerHTML, node) {
return innerHTML + " ";
}
};

export interface Note {
content: string;
title: string;
date: string;
archived: boolean;
tags: string[];
attachments: string[];
}

export function parse(data) {
var $ = cheerio.load(data);

var note = {} as Note;
note.content = $(".content").html();
note.content = toMarkdown(note.content, { converters: [converter] }).trim();

// FIXME: What about timezone?
note.date = $(".heading")
.text()
.trim();
note.date = moment(note.date).toISOString();

note.title = $(".title")
.text()
.trim();
note.archived = $.contains(".archived");

note.tags = $("span.label").toArray().map(function(elem) {
if (!elem.children) {
return null;
}
return elem.children[0].data;
});

var attachments = $("div.attachments").toArray();
note.attachments = getImages(attachments);

return note;
}
Loading

0 comments on commit 5d345d9

Please sign in to comment.