Skip to content

Commit df50195

Browse files
authored
Expose MarkEditGetHtml as a global function (#77)
1 parent 79920f5 commit df50195

4 files changed

Lines changed: 14 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Access it from the `Extensions` menu in the menu bar, or use the keyboard shortc
3131

3232
To display local images, please ensure you're using MarkEdit 1.24.0 or later and follow [the guide](https://github.com/MarkEdit-app/MarkEdit/wiki/Customization#grant-folder-access) to grant file access.
3333

34+
This extension also exposes a global function, `MarkEditGetHtml(styled: boolean) => Promise<string>`, allowing other extensions or scripts to easily generate the rendered HTML.
35+
3436
## Styling
3537

3638
This extension applies the [github-markdown](https://github.com/sindresorhus/github-markdown-css) styling. You can customize the appearance by following the [customization](https://github.com/MarkEdit-app/MarkEdit/wiki/Customization) guidelines.

main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
copyRichText,
1818
getEditPane,
1919
getPreviewPane,
20+
generateStaticHtml,
2021
} from './src/view';
2122

2223
import { enableHoverPreview } from './src/image';
@@ -41,6 +42,9 @@ if (window.__markeditPreviewInitialized__) {
4142
window.__markeditPreviewInitialized__ = true;
4243
}
4344

45+
// Allow other extensions or scripts to generate the HTML
46+
window.MarkEditGetHtml ??= generateStaticHtml;
47+
4448
MarkEdit.addMainMenuItem({
4549
title: localized('viewMode'),
4650
icon: macOSTahoe() ? 'eye' : undefined,

src/view.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ export function getPreviewPane() {
240240
return previewPane;
241241
}
242242

243+
export async function generateStaticHtml(styled: boolean) {
244+
const html = await getRenderedHtml(false);
245+
return styled ? (await applyStyles(html)) : html;
246+
}
247+
243248
async function getRenderedHtml(lineInfo = true) {
244249
const markdown = MarkEdit.editorAPI.getText();
245250
return await renderMarkdown(markdown, lineInfo);
@@ -260,15 +265,8 @@ async function saveGeneratedHtml(styled: boolean) {
260265
return `${getFileName(info.filePath)}.html`;
261266
})();
262267

263-
const fileContent = await (async() => {
264-
const html = await getRenderedHtml(false);
265-
return styled ? (await applyStyles(html)) : html;
266-
})();
267-
268-
MarkEdit.showSavePanel({
269-
fileName,
270-
string: fileContent,
271-
});
268+
const string = await generateStaticHtml(styled);
269+
MarkEdit.showSavePanel({ fileName, string });
272270
}
273271

274272
const states: {

types/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare global {
22
interface Window {
33
__markeditPreviewInitialized__: boolean;
4+
MarkEditGetHtml: (styled: boolean) => Promise<string>;
45
}
56
}
67

0 commit comments

Comments
 (0)