marp-dev-preview 0.1.9 → 0.1.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marp-dev-preview",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "A CLI tool to preview Marp markdown files.",
5
5
  "main": "src/marp-dev-preview.mjs",
6
6
  "type": "module",
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'url';
7
7
 
8
8
  /* Sub-modules */
9
9
  import { createServer } from './server.mjs';
10
- import { initializeMarp, renderMarp as renderMarpInternal } from './marp-utils.mjs';
10
+ import { initializeMarp, getMarp } from './marp-utils.mjs';
11
11
  import { parseArgs } from './args.mjs';
12
12
 
13
13
  const __filename = fileURLToPath(import.meta.url);
@@ -43,7 +43,7 @@ const wss = new WebSocketServer({ port: port + 1 });
43
43
 
44
44
  async function renderMarp() {
45
45
  const md = await fs.readFile(markdownFile, 'utf8');
46
- const { html, css } = renderMarpInternal(md);
46
+ const { html, css } = getMarp().render(md);
47
47
  const customCss = `
48
48
  svg[data-marpit-svg] {
49
49
  margin-bottom:20px !important;
@@ -125,7 +125,7 @@ async function renderMarp() {
125
125
 
126
126
  async function reload(markdown) {
127
127
  try {
128
- const { html, css } = renderMarpInternal(markdown);
128
+ const { html, css } = getMarp().render(markdown);
129
129
  const message = JSON.stringify({
130
130
  type: 'update',
131
131
  html: html,
@@ -147,6 +147,24 @@ chokidar.watch(markdownFile).on('change', async () => {
147
147
  await reload(md);
148
148
  });
149
149
 
150
+ if (themeSet) {
151
+ for (const themeDir of themeSet) {
152
+ console.debug('Watching themes:', path.join(themeDir, '*.css'))
153
+ chokidar.watch(themeDir).on('change', async (file) => {
154
+ // if not a css file, ignore
155
+ console.debug('Detected change in file :', file);
156
+ if (path.extname(file) !== '.css') return;
157
+
158
+ console.debug('Reloading theme from file :', file);
159
+
160
+ console.debug(`Theme file ${file} changed, updating...`);
161
+ await initializeMarp(themeSet);
162
+ const md = await fs.readFile(markdownFile, 'utf8');
163
+ await reload(md);
164
+ });
165
+ }
166
+ }
167
+
150
168
  initializeMarp(themeSet).then(() => {
151
169
  createServer(port, markdownFile, markdownDir, renderMarp, reload, wss, __dirname);
152
170
  }).catch(error => {
@@ -8,6 +8,11 @@ import markdownItContainer from 'markdown-it-container';
8
8
 
9
9
  let marp;
10
10
 
11
+ export function getMarp() {
12
+ return marp;
13
+ }
14
+
15
+
11
16
  export async function initializeMarp(themeSet) {
12
17
  const options = { html: true, linkify: true, };
13
18
  marp = new Marp(options)
@@ -48,6 +53,6 @@ export async function initializeMarp(themeSet) {
48
53
  }
49
54
 
50
55
  export function renderMarp(markdown) {
51
- const { html, css } = marp.render(markdown);
56
+ const { html, css } = getMarp().render(markdown);
52
57
  return { html, css };
53
58
  }