2020-11-07 17:59:37 +02:00
|
|
|
import { PluginStates } from '@joplin/lib/services/plugins/reducer';
|
2020-10-21 01:23:55 +02:00
|
|
|
import { useCallback, useMemo } from 'react';
|
2020-05-02 17:41:07 +02:00
|
|
|
import { ResourceInfos } from './types';
|
2021-01-27 19:42:58 +02:00
|
|
|
import markupLanguageUtils from '../../../utils/markupLanguageUtils';
|
2020-11-07 17:59:37 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2023-03-30 17:58:48 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
const { themeStyle } = require('@joplin/lib/theme');
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
interface HookDependencies {
|
2020-11-12 21:29:22 +02:00
|
|
|
themeId: number;
|
|
|
|
customCss: string;
|
|
|
|
plugins: PluginStates;
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 13:19:53 +02:00
|
|
|
export interface MarkupToHtmlOptions {
|
2020-11-12 21:29:22 +02:00
|
|
|
replaceResourceInternalToExternalLinks?: boolean;
|
|
|
|
resourceInfos?: ResourceInfos;
|
2021-08-14 13:19:53 +02:00
|
|
|
contentMaxWidth?: number;
|
|
|
|
plugins?: Record<string, any>;
|
|
|
|
bodyOnly?: boolean;
|
2021-11-03 14:10:46 +02:00
|
|
|
mapsToLine?: boolean;
|
2022-08-04 11:12:22 +02:00
|
|
|
useCustomPdfViewer?: boolean;
|
2022-08-27 14:32:20 +02:00
|
|
|
noteId?: string;
|
|
|
|
vendorDir?: string;
|
2023-03-30 17:58:48 +02:00
|
|
|
platformName?: string;
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default function useMarkupToHtml(deps: HookDependencies) {
|
2020-10-21 01:23:55 +02:00
|
|
|
const { themeId, customCss, plugins } = deps;
|
|
|
|
|
|
|
|
const markupToHtml = useMemo(() => {
|
2020-12-19 19:42:18 +02:00
|
|
|
return markupLanguageUtils.newMarkupToHtml(deps.plugins, {
|
2020-10-21 01:23:55 +02:00
|
|
|
resourceBaseUrl: `file://${Setting.value('resourceDir')}/`,
|
2021-05-19 15:00:16 +02:00
|
|
|
customCss: customCss || '',
|
2020-10-21 01:23:55 +02:00
|
|
|
});
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2021-07-23 12:05:21 +02:00
|
|
|
}, [plugins, customCss]);
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
return useCallback(async (markupLanguage: number, md: string, options: MarkupToHtmlOptions = null): Promise<any> => {
|
|
|
|
options = {
|
|
|
|
replaceResourceInternalToExternalLinks: false,
|
|
|
|
resourceInfos: {},
|
2023-03-30 17:58:48 +02:00
|
|
|
platformName: shim.platformName(),
|
2020-05-02 17:41:07 +02:00
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
|
|
|
md = md || '';
|
|
|
|
|
|
|
|
const theme = themeStyle(themeId);
|
|
|
|
let resources = {};
|
|
|
|
|
|
|
|
if (options.replaceResourceInternalToExternalLinks) {
|
|
|
|
md = await Note.replaceResourceInternalToExternalLinks(md, { useAbsolutePaths: true });
|
|
|
|
} else {
|
|
|
|
resources = options.resourceInfos;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete options.replaceResourceInternalToExternalLinks;
|
|
|
|
|
|
|
|
const result = await markupToHtml.render(markupLanguage, md, theme, Object.assign({}, {
|
|
|
|
codeTheme: theme.codeThemeCss,
|
|
|
|
resources: resources,
|
|
|
|
postMessageSyntax: 'ipcProxySendToHost',
|
|
|
|
splitted: true,
|
|
|
|
externalAssetsOnly: true,
|
2023-03-07 19:51:29 +02:00
|
|
|
codeHighlightCacheKey: 'useMarkupToHtml',
|
2020-05-02 17:41:07 +02:00
|
|
|
}, options));
|
|
|
|
|
|
|
|
return result;
|
2022-08-19 13:10:04 +02:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2020-10-21 01:23:55 +02:00
|
|
|
}, [themeId, customCss, markupToHtml]);
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|