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';
|
2020-05-02 16:41:07 +01:00
|
|
|
import { ResourceInfos } from './types';
|
2021-01-27 17:42:58 +00:00
|
|
|
import markupLanguageUtils from '../../../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';
|
2020-05-02 16:41:07 +01:00
|
|
|
|
|
|
|
interface HookDependencies {
|
2020-11-12 19:29:22 +00:00
|
|
|
themeId: number;
|
|
|
|
customCss: string;
|
|
|
|
plugins: PluginStates;
|
2020-05-02 16:41:07 +01:00
|
|
|
}
|
|
|
|
|
2021-08-14 12:19:53 +01:00
|
|
|
export interface MarkupToHtmlOptions {
|
2020-11-12 19:29:22 +00:00
|
|
|
replaceResourceInternalToExternalLinks?: boolean;
|
|
|
|
resourceInfos?: ResourceInfos;
|
2021-08-14 12:19:53 +01:00
|
|
|
contentMaxWidth?: number;
|
|
|
|
plugins?: Record<string, any>;
|
|
|
|
bodyOnly?: boolean;
|
2021-11-03 21:10:46 +09:00
|
|
|
mapsToLine?: boolean;
|
2022-08-04 14:42:22 +05:30
|
|
|
useCustomPdfViewer?: boolean;
|
2022-08-27 18:02:20 +05:30
|
|
|
noteId?: string;
|
|
|
|
vendorDir?: string;
|
2023-03-30 21:28:48 +05:30
|
|
|
platformName?: string;
|
2020-05-02 16:41:07 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 19:13:28 +00:00
|
|
|
export default function useMarkupToHtml(deps: HookDependencies) {
|
2020-10-21 00:23:55 +01:00
|
|
|
const { themeId, customCss, plugins } = deps;
|
|
|
|
|
|
|
|
const markupToHtml = useMemo(() => {
|
2020-12-19 17:42:18 +00:00
|
|
|
return markupLanguageUtils.newMarkupToHtml(deps.plugins, {
|
2020-10-21 00:23:55 +01:00
|
|
|
resourceBaseUrl: `file://${Setting.value('resourceDir')}/`,
|
2021-05-19 15:00:16 +02:00
|
|
|
customCss: customCss || '',
|
2020-10-21 00:23:55 +01:00
|
|
|
});
|
2022-08-19 12:10:04 +01:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2021-07-23 11:05:21 +01:00
|
|
|
}, [plugins, customCss]);
|
2020-05-02 16:41:07 +01:00
|
|
|
|
|
|
|
return useCallback(async (markupLanguage: number, md: string, options: MarkupToHtmlOptions = null): Promise<any> => {
|
|
|
|
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);
|
|
|
|
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 17:51:29 +00:00
|
|
|
codeHighlightCacheKey: 'useMarkupToHtml',
|
2020-05-02 16:41:07 +01:00
|
|
|
}, options));
|
|
|
|
|
|
|
|
return result;
|
2022-08-19 12:10:04 +01:00
|
|
|
// eslint-disable-next-line @seiyab/react-hooks/exhaustive-deps -- Old code before rule was applied
|
2020-10-21 00:23:55 +01:00
|
|
|
}, [themeId, customCss, markupToHtml]);
|
2020-05-02 16:41:07 +01:00
|
|
|
}
|