2020-11-07 15:59:37 +00:00
|
|
|
import { PluginStates } from '@joplin/lib/services/plugins/reducer';
|
2020-10-21 00:23:55 +01:00
|
|
|
import { useCallback, useMemo } from 'react';
|
2024-10-27 21:19:38 +00:00
|
|
|
import markupLanguageUtils from '@joplin/lib/utils/markupLanguageUtils';
|
2020-11-07 15:59:37 +00:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2023-03-30 21:28:48 +05:30
|
|
|
import shim from '@joplin/lib/shim';
|
2020-10-21 00:23:55 +01:00
|
|
|
|
2020-11-07 15:59:37 +00:00
|
|
|
const { themeStyle } = require('@joplin/lib/theme');
|
2021-01-22 17:41:11 +00:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
2025-01-13 08:33:42 -08:00
|
|
|
import { ResourceInfos } from '../NoteEditor/utils/types';
|
2024-08-17 04:22:03 -07:00
|
|
|
import { resourceFullPath } from '@joplin/lib/models/utils/resourceUtils';
|
2025-01-13 08:33:42 -08:00
|
|
|
import { RenderOptions } from '@joplin/renderer/types';
|
|
|
|
|
import getPluginSettingValue from '@joplin/lib/services/plugins/utils/getPluginSettingValue';
|
|
|
|
|
import { ScrollbarSize } from '@joplin/lib/models/settings/builtInMetadata';
|
|
|
|
|
|
|
|
|
|
export interface MarkupToHtmlOptions extends RenderOptions {
|
|
|
|
|
resourceInfos?: ResourceInfos;
|
|
|
|
|
replaceResourceInternalToExternalLinks?: boolean;
|
|
|
|
|
}
|
2020-05-02 16:41:07 +01:00
|
|
|
|
|
|
|
|
interface HookDependencies {
|
2020-11-12 19:29:22 +00:00
|
|
|
themeId: number;
|
|
|
|
|
customCss: string;
|
|
|
|
|
plugins: PluginStates;
|
2023-12-29 16:08:09 +00:00
|
|
|
whiteBackgroundNoteRendering: boolean;
|
2025-01-13 08:33:42 -08:00
|
|
|
scrollbarSize: ScrollbarSize;
|
2025-06-07 03:15:59 -07:00
|
|
|
baseFontFamily: string;
|
2020-05-02 16:41:07 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 19:13:28 +00:00
|
|
|
export default function useMarkupToHtml(deps: HookDependencies) {
|
2025-06-07 03:15:59 -07:00
|
|
|
const { themeId, customCss, plugins, whiteBackgroundNoteRendering, scrollbarSize, baseFontFamily } = deps;
|
2020-10-21 00:23:55 +01:00
|
|
|
|
2024-08-17 04:22:03 -07:00
|
|
|
const resourceBaseUrl = useMemo(() => {
|
|
|
|
|
return `joplin-content://note-viewer/${Setting.value('resourceDir')}/`;
|
|
|
|
|
}, []);
|
|
|
|
|
|
2020-10-21 00:23:55 +01:00
|
|
|
const markupToHtml = useMemo(() => {
|
2023-12-29 16:08:09 +00:00
|
|
|
return markupLanguageUtils.newMarkupToHtml(plugins, {
|
2024-08-17 04:22:03 -07:00
|
|
|
resourceBaseUrl,
|
2021-05-19 15:00:16 +02:00
|
|
|
customCss: customCss || '',
|
2020-10-21 00:23:55 +01:00
|
|
|
});
|
2024-08-17 04:22:03 -07:00
|
|
|
}, [plugins, customCss, resourceBaseUrl]);
|
2020-05-02 16:41:07 +01:00
|
|
|
|
2025-01-13 08:33:42 -08:00
|
|
|
return useCallback(async (markupLanguage: number, md: string, options: MarkupToHtmlOptions|null = null) => {
|
2020-05-02 16:41:07 +01:00
|
|
|
options = {
|
|
|
|
|
replaceResourceInternalToExternalLinks: false,
|
|
|
|
|
resourceInfos: {},
|
2023-03-30 21:28:48 +05:30
|
|
|
platformName: shim.platformName(),
|
2020-05-02 16:41:07 +01:00
|
|
|
...options,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
md = md || '';
|
|
|
|
|
|
|
|
|
|
const theme = themeStyle(themeId);
|
2024-08-17 04:22:03 -07:00
|
|
|
let resources: ResourceInfos = {};
|
2020-05-02 16:41:07 +01:00
|
|
|
|
|
|
|
|
if (options.replaceResourceInternalToExternalLinks) {
|
|
|
|
|
md = await Note.replaceResourceInternalToExternalLinks(md, { useAbsolutePaths: true });
|
|
|
|
|
} else {
|
|
|
|
|
resources = options.resourceInfos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete options.replaceResourceInternalToExternalLinks;
|
|
|
|
|
|
2023-11-03 19:45:21 +00:00
|
|
|
const result = await markupToHtml.render(markupLanguage, md, theme, {
|
|
|
|
|
codeTheme: theme.codeThemeCss,
|
2020-05-02 16:41:07 +01:00
|
|
|
resources: resources,
|
|
|
|
|
postMessageSyntax: 'ipcProxySendToHost',
|
|
|
|
|
splitted: true,
|
|
|
|
|
externalAssetsOnly: true,
|
2023-11-03 19:45:21 +00:00
|
|
|
codeHighlightCacheKey: 'useMarkupToHtml',
|
2025-01-13 08:33:42 -08:00
|
|
|
settingValue: getPluginSettingValue,
|
2023-12-29 16:08:09 +00:00
|
|
|
whiteBackgroundNoteRendering,
|
2025-01-13 08:33:42 -08:00
|
|
|
scrollbarSize: scrollbarSize,
|
2025-06-07 03:15:59 -07:00
|
|
|
baseFontFamily,
|
2024-08-17 04:22:03 -07:00
|
|
|
itemIdToUrl: (id: string, urlParameters = '') => {
|
|
|
|
|
if (!(id in resources) || !resources[id]) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resourceFullPath(resources[id].item, resourceBaseUrl) + urlParameters;
|
|
|
|
|
},
|
2025-11-15 09:11:29 +00:00
|
|
|
globalSettings: {
|
|
|
|
|
'markdown.plugin.abc.options': Setting.value('markdown.plugin.abc.options'),
|
|
|
|
|
},
|
2023-11-03 19:45:21 +00:00
|
|
|
...options,
|
|
|
|
|
});
|
2020-05-02 16:41:07 +01:00
|
|
|
|
|
|
|
|
return result;
|
2025-06-07 03:15:59 -07:00
|
|
|
}, [themeId, markupToHtml, baseFontFamily, whiteBackgroundNoteRendering, scrollbarSize, resourceBaseUrl]);
|
2020-05-02 16:41:07 +01:00
|
|
|
}
|