Skip to content

Commit

Permalink
feat: add tool lib for api
Browse files Browse the repository at this point in the history
provide tools to convert url/json to ReturnBody, and compare version
  • Loading branch information
aidenlx committed Aug 23, 2021
1 parent 13e4129 commit 3306325
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@ node_modules
package-lock.json

# build
dist
dist
lib
13 changes: 9 additions & 4 deletions package.json
Expand Up @@ -2,13 +2,14 @@
"name": "@alx-plugins/obsidian-bridge",
"version": "2.1.0",
"description": "MarginNote 3 --> Obsidian",
"types": "src/modules/return.d.ts",
"main": "",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"src/modules/return.d.ts"
"lib/**/*"
],
"scripts": {
"build": "rollup --config rollup.config.js",
"dev": "rollup --config rollup.config.js",
"build": "rollup --config rollup.config.js --environment BUILD:production",
"prettier": "prettier --write 'src/**/*.+(ts|tsx|json|html|css)'",
"eslint": "eslint . --ext .ts,.tsx --fix",
"release": "release-it"
Expand All @@ -27,7 +28,9 @@
"@types/url-parse": "^1.4.4",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.29.0",
"@zerollup/ts-transform-paths": "^1.7.18",
"assert-never": "^1.2.1",
"compare-versions": "^3.6.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
Expand All @@ -43,10 +46,12 @@
"release-it": "^14.11.5",
"rollup": "^2.56.2",
"rollup-plugin-copy2": "^0.3.1",
"rollup-plugin-typescript2": "^0.30.0",
"rollup-plugin-zip": "^1.0.2",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"tslib": "^2.3.1",
"ttypescript": "^1.5.12",
"typescript": "^4.3.5",
"url-parse": "^1.5.3"
},
Expand Down
56 changes: 42 additions & 14 deletions rollup.config.js
Expand Up @@ -2,32 +2,60 @@ import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import copy from "rollup-plugin-copy2";
import tsPlugin from "rollup-plugin-typescript2";
import zip from "rollup-plugin-zip";
import ttypescript from "ttypescript";

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/
`;

export default {
input: "src/main.ts",
const isProd = process.env.BUILD === "production";

const configs = [
{
input: "src/main.ts",
output: {
dir: "dist/",
format: "iife",
exports: "none",
sourcemap: "hidden",
banner,
},
plugins: [
typescript(),
nodeResolve({ browser: true }),
commonjs(),
copy({
assets: ["mnaddon.json", ["assets/title.png", "title.png"]],
}),
zip({
file: "ob-bridge.mnaddon",
}),
],
},
];

const libCfg = {
input: "src/index.ts",
output: {
dir: "dist/",
format: "iife",
exports: "none",
sourcemap: "hidden",
banner,
dir: "lib",
sourcemap: true,
format: "cjs",
},
external: ["obsidian"],
plugins: [
typescript(),
tsPlugin({
tsconfig: "tsconfig-lib.json",
typescript: ttypescript,
}),
nodeResolve({ browser: true }),
commonjs(),
copy({
assets: ["mnaddon.json", ["assets/title.png", "title.png"]],
}),
zip({
file: "ob-bridge.mnaddon",
}),
],
};

if (isProd) configs.push(libCfg);

export default configs;
29 changes: 29 additions & 0 deletions src/const.ts
@@ -0,0 +1,29 @@
import compareVersions from "compare-versions";

const version = "2.1.0";
export const VERSION: string = version;
export const PREFIX = `<!--MN^${version}-->\n`;
export const PREFIX_REGEX = /^<!--MN\^([\d.]+)-->\n/;

/**
* @returns null if invaild, 1 if given higher version, -1 if given lower version
*/
export const checkVersion = (
verStr: string,
toCompare?: string,
): number | null => {
if (toCompare && !compareVersions.validate(toCompare)) {
console.error("invaild version given to compare: " + toCompare);
return null;
}
let ver: string, match;
if ((match = verStr.match(PREFIX_REGEX))) {
ver = match[1];
} else ver = verStr;
if (compareVersions.validate(ver)) {
return compareVersions(ver, toCompare ?? version);
} else {
console.error("invaild version %s extracted from %s", ver, verStr);
return null;
}
};
14 changes: 14 additions & 0 deletions src/index.ts
@@ -0,0 +1,14 @@
export { checkVersion, PREFIX } from "./const";
export {
Book,
Data,
DataType,
Note,
ReturnBody,
ReturnBody_Note,
ReturnBody_Sel,
ReturnBody_Toc,
Selection,
Toc,
} from "./return";
export { UrlToObj } from "modules/url-obj";
2 changes: 1 addition & 1 deletion src/modules/PopupRecorder.ts
@@ -1,4 +1,4 @@
import { inHistory, item, time } from "./return";
import { inHistory, item, time } from "../return";

export default class PopupRecorder {
private history: Array<inHistory>;
Expand Down
4 changes: 0 additions & 4 deletions src/modules/const.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/modules/parser.ts
@@ -1,6 +1,5 @@
import { excerptPic_video, MbBook, MbBookNote } from "@alx-plugins/marginnote";
import { excerptPic_video, MbBookNote } from "@alx-plugins/marginnote";

import PopupRecorder from "./PopupRecorder";
import {
Book,
Data,
Expand All @@ -9,7 +8,8 @@ import {
ReturnBody_Sel,
ReturnBody_Toc,
Selection,
} from "./return";
} from "../return";
import PopupRecorder from "./PopupRecorder";
import { scanNote, scanObject, scanToc } from "./scan";

const getBook = (docMd5: string | undefined): Book | null => {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/scan.ts
@@ -1,6 +1,6 @@
import { MbBookNote } from "@alx-plugins/marginnote";

import { Note, Toc } from "./return";
import { Note, Toc } from "../return";
import { RequiredKeys } from "./type-tools";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/modules/sender.ts
@@ -1,7 +1,7 @@
import { MbBookNote } from "@alx-plugins/marginnote";

import { ReturnBody, Selection } from "../return";
import { getBody_Note, getBody_Sel, getBody_Toc } from "./parser";
import { ReturnBody, Selection } from "./return";
import { showHUD } from "./tools";
import getText from "./translate";
import { ObjToUrl } from "./url-obj";
Expand Down
70 changes: 67 additions & 3 deletions src/modules/url-obj.ts
Expand Up @@ -4,11 +4,11 @@ import { ObsidianProtocolData } from "obsidian";
import { stringify as toQs } from "query-string";
import URLParse from "url-parse";

import { VERSION } from "./const";
import { DataType, ReturnBody } from "./return";
import { PREFIX, PREFIX_REGEX, VERSION } from "../const";
import { DataType, ReturnBody } from "../return";
import { copy } from "./tools";

export const ObjToUrl = (obj: ReturnBody) => {
export const ObjToUrl = (obj: ReturnBody): string => {
// copy(JSON.stringify(obj));
const url = URLParse("obsidian://mncomp"),
{ type, sendTime, last, data } = obj,
Expand All @@ -23,4 +23,68 @@ export const ObjToUrl = (obj: ReturnBody) => {
url.set("query", qs);
return url.toString();
};
export const ObjToJson = (obj: ReturnBody): string =>
PREFIX + JSON.stringify(obj);

type QsObj = {
version: string;
type: DataType;
sendTime: number;
last: string;
data: string;
};
const dts = ["sel", "note", "toc"];
const keys: (keyof QsObj)[] = ["version", "type", "sendTime", "last", "data"];
const isDataType = (str: string): str is DataType => dts.includes(str);

export const UrlToObj = (
params: ObsidianProtocolData,
): [version: string, body: ReturnBody] | null => {
let obj = {} as any,
version: string = "";
for (const key of keys) {
if (!params[key]) {
console.error("param empty, key: " + key);
return null;
} else {
switch (key) {
case "version":
version = params[key];
continue;
case "type": {
let type = params[key];
if (isDataType(type)) obj[key] = type;
else {
console.error("invaild type: " + type);
return null;
}
continue;
}
case "sendTime":
obj[key] = +params[key];
continue;
case "last":
case "data":
obj[key] = JsonURL.parse(params[key]);
continue;
default:
assertNever(key);
}
}
}
if (version === "") {
console.error("version empty");
return null;
}
return [version, obj as ReturnBody];
};
export const JsonToObj = (
src: string,
): [version: string, body: ReturnBody] | null => {
let match;
if ((match = src.match(PREFIX_REGEX))) {
const version = match[1];
const json = src.replace(PREFIX_REGEX, "");
return [version, JSON.parse(json)];
} else return null;
};
4 changes: 1 addition & 3 deletions src/modules/return.d.ts → src/return.d.ts
@@ -1,6 +1,6 @@
import { MbBook, MbBookNote } from "@alx-plugins/marginnote";

import { DateCvt, NonTypeProps, TypePropNames } from "./type-tools";
import { DateCvt, NonTypeProps, TypePropNames } from "./modules/type-tools";

export type Book = DateCvt<MbBook>;
type note_valOnly = NonTypeProps<MbBookNote, Function>;
Expand All @@ -25,8 +25,6 @@ export type item = {
export type Data = Selection | Note | Toc;
export type DataType = "sel" | "note" | "toc";

export type MNMark = "<!--MN-->\n";

type ReturnBody_Basic = {
type: DataType;
sendTime: ReturnType<typeof Date.now>;
Expand Down
20 changes: 20 additions & 0 deletions tsconfig-lib.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"baseUrl": "src",
"module": "ESNext",
"target": "es6",
"allowJs": true,
"sourceMap": true,
"noImplicitAny": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"importHelpers": true,
"strict": true,
"declaration": true,
"lib": ["ES2020"],
"outDir": "lib",
"plugins": [{ "transform": "@zerollup/ts-transform-paths" }]
},
"include": ["src/**/*.ts"]
}

0 comments on commit 3306325

Please sign in to comment.