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

Commit

Permalink
Typescript: Make it stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
vHanda committed Sep 11, 2018
1 parent 1cfdda6 commit 00c06e1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 70 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"author": "",
"dependencies": {
"cheerio": "0.22.0",
"json2yaml": "^1.1.0",
"moment": "^2.14.1",
"to-markdown": "3.0.1",
"uuid": "^3.0.1"
},
"devDependencies": {
"@types/cheerio": "^0.22.9",
"@types/jest": "^23.3.2",
"@types/node": "^10.9.4",
"jest": "^23.6.0",
Expand Down
19 changes: 10 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node

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

import { parse } from "./parser";
import { serialize } from "./serializer";

// TODO: parse argv and get directory file list
// parse output
Expand All @@ -23,17 +24,17 @@ if (process.argv.length != 4) {
var inputDir = process.argv[2];
var outputDir = process.argv[3];

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

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

var files = fs.readdirSync(inputDir);
var files = fs.readdirSync(inputDir) as string[];
files = files.filter(t => t.endsWith(".html"));
files.forEach(filePath => convert(inputDir + "/" + filePath, outputDir));
37 changes: 16 additions & 21 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ var cheerio = require("cheerio");
var toMarkdown = require("to-markdown");
var moment = require("moment");

function getImages(node) {
var images = [];
import { Note } from "./types";

function getImages(node: any) {
var images: string[] = [];
if (node instanceof Array) {
node.forEach(child => {
images = images.concat(getImages(child));
Expand All @@ -19,7 +21,7 @@ function getImages(node) {

if (!node.children) return [];

node.children.forEach(child => {
node.children.forEach((child: any) => {
images = images.concat(getImages(child));
});
return images;
Expand All @@ -32,21 +34,12 @@ var converter = {
return node.className.indexOf('listitem') != -1;
},
*/
replacement: function(innerHTML, node) {
replacement: function(innerHTML: string, node: any) {
return innerHTML + " ";
}
};

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

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

var note = {} as Note;
Expand All @@ -64,15 +57,17 @@ export function parse(data) {
.trim();
note.archived = $.contains(".archived");

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

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

return note;
}
}
74 changes: 37 additions & 37 deletions src/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
var fs = require("fs");
var uuidV4 = require("uuid/v4");
var YAML = require("json2yaml");

/**
* Takes a note and serializes it into [(fileName, content)]
*/
import { Note } from "./types";

interface SerializedNote {
fileName: string;
content: string | Buffer;
}

export function serialize(note: Note): SerializedNote[] {
var serializedNotes: SerializedNote[] = [];

var serialize = function(note) {
// FIXME: Serialize the attachments!
var out = note.attachments.map(generateAttachment);
var mainOutput = generateOutputFile(note);
var out = note.attachments.map(generateAttachment);
out.forEach(a => {
var fileName = a[0];
var fileName = a[0].toString();
mainOutput += "\n![](./" + fileName + ")\n";
});
out.push([generateFilename(note), mainOutput]);
return out;
};

var generateYamlFrontMatter = function(note) {
var lines = ["---"];
for (var key in note) {
if (!note.hasOwnProperty(key)) continue;

if (key == "content" || key == "attachments") continue;
serializedNotes.push({
fileName: fileName,
content: a[1]
});
});

var val = note[key];
if (val instanceof Array) {
if (val.length == 0) continue;
val = "[" + val.join(", ") + "]";
} else {
val = "" + val; // convert to string
}
if (val.trim().length == 0 || val == "false") continue;
serializedNotes.push({
fileName: generateFilename(note),
content: mainOutput
});
return serializedNotes;
}

lines.push(key + ": " + val);
}
lines.push("---");
var generateYamlFrontMatter = function(note: Note) {
var obj: any = note;
delete obj.content;
delete obj.attachments;

return lines.join("\n");
const ymlText = YAML.stringify(obj);
return "---\n" + ymlText + "---";
};

function generateOutputFile(note) {
function generateOutputFile(note: Note) {
return generateYamlFrontMatter(note) + "\n" + note.content;
}

function generateFilename(note) {
function sanitizeString(str) {
function generateFilename(note: Note) {
function sanitizeString(str: string) {
var newStr = "";
var re = /[A-Za-z0-9- ]/;
for (var key in str) {
var char = str[key];
if (char.match(re))
for (var x = 0; x < str.length; x++) {
let char = str[x];
if (char.match(re)) {
if (char == " ") newStr += "-";
else newStr += char;
}
}
return newStr;
}
return sanitizeString(note.title || note.date || uuidV4()) + ".md";
}

function generateAttachment(a) {
function generateAttachment(a: string) {
var regex = /^data:.+\/(.+);base64,(.*)$/;
var matches = a.substr(0, 100).match(regex);
var ext = matches[1];
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Note {
content: string;
title: string;
date: string;
archived: boolean;
tags: string[];
attachments: string[];
}
14 changes: 11 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
{
"compilerOptions": {
"outDir": "./dist",
"allowJs": true,
"target": "es5"
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
},
"include": ["./src/**/*"]
}

0 comments on commit 00c06e1

Please sign in to comment.