diff --git a/package.json b/package.json index 9479e55..4550512 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/main.ts b/src/main.ts index 6578b91..c1932c7 100755 --- a/src/main.ts +++ b/src/main.ts @@ -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 @@ -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)); diff --git a/src/parser.ts b/src/parser.ts index e0debfe..11d8d92 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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)); @@ -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; @@ -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; @@ -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; -} \ No newline at end of file +} diff --git a/src/serializer.ts b/src/serializer.ts index d28f81a..d9fb3e9 100644 --- a/src/serializer.ts +++ b/src/serializer.ts @@ -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]; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..ce09283 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,8 @@ +export interface Note { + content: string; + title: string; + date: string; + archived: boolean; + tags: string[]; + attachments: string[]; +} diff --git a/tsconfig.json b/tsconfig.json index 25b0d31..6905547 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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/**/*"] }