Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Access it from the `Extensions` menu in the menu bar, or use the keyboard shortc

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.

This extension also exposes a global function, `MarkEditGetHtml(styled: boolean) => Promise<string>`, allowing other extensions or scripts to easily generate the rendered HTML.
Comment thread
cyanzhong marked this conversation as resolved.

## Styling

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.
Expand Down
4 changes: 4 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
copyRichText,
getEditPane,
getPreviewPane,
generateStaticHtml,
} from './src/view';

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

// Allow other extensions or scripts to generate the HTML
window.MarkEditGetHtml ??= generateStaticHtml;

MarkEdit.addMainMenuItem({
title: localized('viewMode'),
icon: macOSTahoe() ? 'eye' : undefined,
Expand Down
16 changes: 7 additions & 9 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export function getPreviewPane() {
return previewPane;
}

export async function generateStaticHtml(styled: boolean) {
const html = await getRenderedHtml(false);
return styled ? (await applyStyles(html)) : html;
}

async function getRenderedHtml(lineInfo = true) {
const markdown = MarkEdit.editorAPI.getText();
return await renderMarkdown(markdown, lineInfo);
Expand All @@ -260,15 +265,8 @@ async function saveGeneratedHtml(styled: boolean) {
return `${getFileName(info.filePath)}.html`;
})();

const fileContent = await (async() => {
const html = await getRenderedHtml(false);
return styled ? (await applyStyles(html)) : html;
})();

MarkEdit.showSavePanel({
fileName,
string: fileContent,
});
const string = await generateStaticHtml(styled);
MarkEdit.showSavePanel({ fileName, string });
}

const states: {
Expand Down
1 change: 1 addition & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare global {
interface Window {
__markeditPreviewInitialized__: boolean;
MarkEditGetHtml: (styled: boolean) => Promise<string>;
}
}

Expand Down
Loading