@mryhryki/markdown-preview 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
 
4
- require("./src/index");
4
+ require("./src/index.js");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mryhryki/markdown-preview",
3
3
  "description": "Markdown realtime preview on browser",
4
- "version": "0.4.3",
4
+ "version": "0.5.0",
5
5
  "author": "mryhryki",
6
6
  "license": "MIT",
7
7
  "publishConfig": {
@@ -20,8 +20,8 @@
20
20
  "url": "https://github.com/mryhryki/markdown-preview/issues"
21
21
  },
22
22
  "engines": {
23
- "node": ">=12.0.0",
24
- "npm": ">=6.0.0"
23
+ "node": ">=18.0.0",
24
+ "npm": ">=9.0.0"
25
25
  },
26
26
  "main": "index.js",
27
27
  "bin": {
@@ -33,19 +33,38 @@
33
33
  "log4js": "^6.9.1",
34
34
  "opener": "^1.5.2",
35
35
  "serve-index": "^1.9.1",
36
- "ws": "^8.13.0"
36
+ "ws": "^8.14.2"
37
37
  },
38
38
  "devDependencies": {
39
- "@mryhryki/lint": "^0.0.12",
40
- "jest": "^29.5.0",
41
- "nodemon": "^2.0.21"
39
+ "@types/express": "^4.17.17",
40
+ "@types/express-ws": "^3.0.1",
41
+ "@types/jest": "^29.5.5",
42
+ "@types/opener": "^1.4.1",
43
+ "@types/serve-index": "^1.9.1",
44
+ "eslint": "^8.49.0",
45
+ "eslint-config-prettier": "^9.0.0",
46
+ "eslint-plugin-jest": "^27.4.0",
47
+ "eslint-plugin-node": "^11.1.0",
48
+ "eslint-plugin-prettier": "^5.0.0",
49
+ "eslint-plugin-react": "^7.33.2",
50
+ "eslint-plugin-react-hooks": "^4.6.0",
51
+ "eslint-plugin-simple-import-sort": "^10.0.0",
52
+ "eslint-plugin-unused-imports": "^3.0.0",
53
+ "jest": "^29.7.0",
54
+ "nodemon": "^3.0.1",
55
+ "ts-jest": "^29.1.1",
56
+ "typescript": "^5.2.2"
42
57
  },
43
58
  "scripts": {
44
- "start": "node ./index.js",
45
- "dev": "nodemon --watch ./src/ index.js --no-opener --log-level debug",
46
- "lint": "mryhryki-lint",
47
- "lint:fix": "mryhryki-lint --fix",
59
+ "build": "tsc",
60
+ "dev": "nodemon --watch ./src/ --ext 'ts' --exec 'npm start -- --no-opener --log-level debug'",
61
+ "lint": "eslint ./src/",
62
+ "lint:fix": "eslint ./src/ --fix",
63
+ "release": "npm run build && npm publish",
64
+ "start": "npm run build && node ./index.js",
48
65
  "test": "jest",
49
- "test:watch": "jest --watchAll"
66
+ "test:watch": "jest --watchAll",
67
+ "type": "tsc --noEmit",
68
+ "type:watch": "tsc --noEmit --watch"
50
69
  }
51
70
  }
package/src/index.js CHANGED
@@ -1,46 +1,49 @@
1
1
  "use strict";
2
-
3
- const express = require("express");
4
- const expressWs = require("express-ws");
5
- const serveIndex = require("serve-index");
6
- const opener = require("opener");
7
- const getLogger = require("./lib/logger");
8
- const { showUsage, showVersion } = require("./lib/show");
9
- const MarkdownHandler = require("./markdown");
10
- const WebSocketHandler = require("./websocket");
11
- const { rootDir, staticDir } = require("./lib/directory");
12
- const Params = require("./lib/params");
13
-
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const express_1 = __importDefault(require("express"));
7
+ const express_ws_1 = __importDefault(require("express-ws"));
8
+ const serve_index_1 = __importDefault(require("serve-index"));
9
+ const opener_1 = __importDefault(require("opener"));
10
+ const logger_1 = require("./lib/logger");
11
+ const show_1 = require("./lib/show");
12
+ const markdown_1 = require("./markdown");
13
+ const websocket_1 = require("./websocket");
14
+ const directory_1 = require("./lib/directory");
15
+ const params_1 = require("./lib/params");
14
16
  try {
15
- const params = new Params(process.env, process.argv.slice(2));
16
- if (params.help) showUsage();
17
- if (params.version) showVersion();
18
-
19
- const logger = getLogger(params.logLevel);
20
- const previewUrl = `http://localhost:${params.port}`;
21
-
22
- console.log("Root Directory :", rootDir);
23
- console.log("Default File :", params.filepath);
24
- console.log("Extensions :", params.extensions.join(", "));
25
- console.log("Template File :", params.template);
26
- console.log(`Preview URL : ${previewUrl}`);
27
-
28
- const app = express();
29
- expressWs(app);
30
- app.get("/", (_req, res) => res.redirect(params.filepath));
31
- app.ws("/ws", WebSocketHandler(logger));
32
- params.extensions.forEach((ext) => {
33
- app.get(new RegExp(`^/.+\.${ext}$`), MarkdownHandler(params.template));
34
- });
35
- app.use(express.static(rootDir, { index: false }));
36
- app.use(express.static(staticDir, { index: false }));
37
- app.use(serveIndex(rootDir, { icons: true, view: "details" }));
38
- app.listen(params.port);
39
-
40
- if (!params.noOpener) {
41
- opener(previewUrl);
42
- }
43
- } catch (err) {
44
- console.error(err.message);
45
- showUsage(true);
17
+ const params = new params_1.Params(process.env, process.argv.slice(2));
18
+ if (params.help)
19
+ (0, show_1.showUsage)();
20
+ if (params.version)
21
+ (0, show_1.showVersion)();
22
+ const logger = (0, logger_1.getLogger)(params.logLevel);
23
+ const previewUrl = `http://localhost:${params.port}`;
24
+ console.log("Root Directory :", directory_1.rootDir);
25
+ console.log("Default File :", params.filepath);
26
+ console.log("Extensions :", params.extensions.join(", "));
27
+ console.log("Template File :", params.template);
28
+ console.log(`Preview URL : ${previewUrl}`);
29
+ const app = (0, express_1.default)();
30
+ (0, express_ws_1.default)(app);
31
+ app.get("/", (_req, res) => res.redirect(params.filepath));
32
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
+ // @ts-ignore
34
+ app.ws("/ws", (0, websocket_1.WebSocketHandler)(logger));
35
+ params.extensions.forEach((ext) => {
36
+ app.get(new RegExp(`^/.+\.${ext}$`), (0, markdown_1.MarkdownHandler)(params.template));
37
+ });
38
+ app.use(express_1.default.static(directory_1.rootDir, { index: false }));
39
+ app.use(express_1.default.static(directory_1.staticDir, { index: false }));
40
+ app.use((0, serve_index_1.default)(directory_1.rootDir, { icons: true, view: "details" }));
41
+ app.listen(params.port);
42
+ if (!params.noOpener) {
43
+ (0, opener_1.default)(previewUrl);
44
+ }
45
+ }
46
+ catch (err) {
47
+ console.error(err);
48
+ (0, show_1.showUsage)(true);
46
49
  }
@@ -1,15 +1,11 @@
1
1
  "use strict";
2
-
3
- const path = require("path");
4
-
5
- const rootDir = process.cwd();
6
- const projectDir = path.resolve(__dirname, "..", "..");
7
- const staticDir = path.resolve(projectDir, "static");
8
- const templateDir = path.resolve(projectDir, "template");
9
-
10
- module.exports = {
11
- rootDir,
12
- projectDir,
13
- staticDir,
14
- templateDir,
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.templateDir = exports.staticDir = exports.projectDir = exports.rootDir = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ exports.rootDir = process.cwd();
9
+ exports.projectDir = path_1.default.resolve(__dirname, "..", "..");
10
+ exports.staticDir = path_1.default.resolve(exports.projectDir, "static");
11
+ exports.templateDir = path_1.default.resolve(exports.projectDir, "template");
package/src/lib/file.js CHANGED
@@ -1,16 +1,17 @@
1
1
  "use strict";
2
-
3
- const fs = require("fs");
4
-
5
- const existsFile = (filepath) => {
6
- try {
7
- fs.statSync(filepath);
8
- return true;
9
- } catch (_) {
10
- return false;
11
- }
12
- };
13
-
14
- module.exports = {
15
- existsFile,
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
4
  };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.existsFile = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ function existsFile(filepath) {
9
+ try {
10
+ fs_1.default.statSync(filepath);
11
+ return true;
12
+ }
13
+ catch (_) {
14
+ return false;
15
+ }
16
+ }
17
+ exports.existsFile = existsFile;
@@ -1,59 +1,59 @@
1
1
  "use strict";
2
-
3
- const fs = require("fs");
4
- const path = require("path");
5
- const { rootDir } = require("./directory");
6
-
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FileWatcher = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const directory_1 = require("./directory");
7
10
  class FileWatcher {
8
- constructor(logger) {
9
- this.logger = logger;
10
- this._target = {};
11
- setInterval(() => {
12
- Object.keys(this._target).forEach((filepath) => {
13
- try {
14
- const fileinfo = this._target[filepath];
15
- const currentLastModified = this.getFileLastModified(filepath);
16
- if (fileinfo.lastModified !== currentLastModified) {
17
- this.logger.info("File update:", path.resolve(rootDir, filepath));
18
- fileinfo.lastModified = currentLastModified;
19
- if (this._onFileChanged != null) {
20
- this._onFileChanged(this.getFileInfo(filepath));
21
- }
22
- }
23
- } catch (err) {
24
- console.error(err);
25
- }
26
- });
27
- }, 250 /* check 4 times per second */);
28
- }
29
-
30
- onFileChanged(callback) {
31
- this._onFileChanged = callback;
32
- }
33
-
34
- addTargetFile(filepath) {
35
- if (this._target[filepath] != null) return;
36
- this.logger.debug("Add watch target:", filepath);
37
- this._target[filepath] = {
38
- lastModified: this.getFileLastModified(filepath),
39
- };
40
- }
41
-
42
- removeTargetFile(filepath) {
43
- if (this._target[filepath] == null) return;
44
- this.logger.debug("Remove watch target:", filepath);
45
- delete this._target[filepath];
46
- }
47
-
48
- getFileLastModified(filepath) {
49
- return fs.statSync(path.resolve(rootDir, filepath)).mtimeMs;
50
- }
51
-
52
- getFileInfo(filepath) {
53
- const absolutePath = path.resolve(rootDir, filepath);
54
- const markdown = fs.readFileSync(absolutePath, "utf-8");
55
- return { filepath, markdown };
56
- }
11
+ constructor(logger) {
12
+ this.logger = logger;
13
+ this._target = {};
14
+ setInterval(() => {
15
+ Object.keys(this._target).forEach((filepath) => {
16
+ try {
17
+ const fileinfo = this._target[filepath];
18
+ const currentLastModified = this.getFileLastModified(filepath);
19
+ if (fileinfo.lastModified !== currentLastModified) {
20
+ this.logger.info("File update:", path_1.default.resolve(directory_1.rootDir, filepath));
21
+ fileinfo.lastModified = currentLastModified;
22
+ if (this._onFileChanged != null) {
23
+ this._onFileChanged(this.getFileInfo(filepath));
24
+ }
25
+ }
26
+ }
27
+ catch (err) {
28
+ console.error(err);
29
+ }
30
+ });
31
+ }, 250 /* check 4 times per second */);
32
+ }
33
+ onFileChanged(callback) {
34
+ this._onFileChanged = callback;
35
+ }
36
+ addTargetFile(filepath) {
37
+ if (this._target[filepath] != null)
38
+ return;
39
+ this.logger.debug("Add watch target:", filepath);
40
+ this._target[filepath] = {
41
+ lastModified: this.getFileLastModified(filepath),
42
+ };
43
+ }
44
+ removeTargetFile(filepath) {
45
+ if (this._target[filepath] == null)
46
+ return;
47
+ this.logger.debug("Remove watch target:", filepath);
48
+ delete this._target[filepath];
49
+ }
50
+ getFileLastModified(filepath) {
51
+ return fs_1.default.statSync(path_1.default.resolve(directory_1.rootDir, filepath)).mtimeMs;
52
+ }
53
+ getFileInfo(filepath) {
54
+ const absolutePath = path_1.default.resolve(directory_1.rootDir, filepath);
55
+ const markdown = fs_1.default.readFileSync(absolutePath, "utf-8");
56
+ return { filepath, markdown };
57
+ }
57
58
  }
58
-
59
- module.exports = FileWatcher;
59
+ exports.FileWatcher = FileWatcher;
package/src/lib/logger.js CHANGED
@@ -1,23 +1,42 @@
1
- const log4js = require("log4js");
2
-
3
- const getLogger = (logLevel) => {
4
- log4js.configure({
5
- appenders: {
6
- console: {
7
- type: "console",
8
- layout: {
9
- type: "basic",
10
- },
11
- },
12
- },
13
- categories: {
14
- default: {
15
- appenders: ["console"],
16
- level: logLevel,
17
- },
18
- },
19
- });
20
- return log4js.getLogger();
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
21
4
  };
22
-
23
- module.exports = getLogger;
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getLogger = exports.getLogLevel = void 0;
7
+ const log4js_1 = __importDefault(require("log4js"));
8
+ const LogLevels = ["trace", "debug", "info", "warn", "error", "fatal"];
9
+ function getLogLevel(level) {
10
+ switch (level) {
11
+ case "trace":
12
+ case "debug":
13
+ case "info":
14
+ case "warn":
15
+ case "error":
16
+ case "fatal":
17
+ return level;
18
+ default:
19
+ throw new Error(`Undefined log level: ${level}`);
20
+ }
21
+ }
22
+ exports.getLogLevel = getLogLevel;
23
+ function getLogger(logLevel) {
24
+ log4js_1.default.configure({
25
+ appenders: {
26
+ console: {
27
+ type: "console",
28
+ layout: {
29
+ type: "basic",
30
+ },
31
+ },
32
+ },
33
+ categories: {
34
+ default: {
35
+ appenders: ["console"],
36
+ level: logLevel,
37
+ },
38
+ },
39
+ });
40
+ return log4js_1.default.getLogger();
41
+ }
42
+ exports.getLogger = getLogger;