2020-11-05 18:58:23 +02:00
|
|
|
import markdownUtils from './markdownUtils';
|
|
|
|
import Setting from './models/Setting';
|
|
|
|
import shim from './shim';
|
2021-05-19 15:00:16 +02:00
|
|
|
import MarkupToHtml, { MarkupLanguage, Options } from '@joplin/renderer/MarkupToHtml';
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2021-01-30 13:08:11 +02:00
|
|
|
import htmlUtils from './htmlUtils';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Resource from './models/Resource';
|
2021-05-19 15:00:16 +02:00
|
|
|
import { PluginStates } from './services/plugins/reducer';
|
2019-07-14 17:00:02 +02:00
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
export class MarkupLanguageUtils {
|
2021-04-24 20:23:33 +02:00
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
private lib_(language: MarkupLanguage) {
|
2020-10-21 01:23:55 +02:00
|
|
|
if (language === MarkupLanguage.Html) return htmlUtils;
|
|
|
|
if (language === MarkupLanguage.Markdown) return markdownUtils;
|
2019-09-19 23:51:18 +02:00
|
|
|
throw new Error(`Unsupported markup language: ${language}`);
|
2019-07-14 17:00:02 +02:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:35:28 +02:00
|
|
|
public extractImageUrls(language: MarkupLanguage, text: string): string[] {
|
|
|
|
let urls: string[] = [];
|
|
|
|
|
|
|
|
if (language === MarkupLanguage.Any) {
|
|
|
|
urls = urls.concat(this.lib_(MarkupLanguage.Markdown).extractImageUrls(text));
|
|
|
|
urls = urls.concat(this.lib_(MarkupLanguage.Html).extractImageUrls(text));
|
|
|
|
} else {
|
|
|
|
urls = this.lib_(language).extractImageUrls(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls;
|
2019-07-14 17:00:02 +02:00
|
|
|
}
|
2019-12-29 19:58:40 +02:00
|
|
|
|
2022-06-20 14:56:54 +02:00
|
|
|
public extractPdfUrls(language: MarkupLanguage, text: string): string[] {
|
|
|
|
let urls: string[] = [];
|
|
|
|
if (language === MarkupLanguage.Any) {
|
|
|
|
urls = urls.concat(this.lib_(MarkupLanguage.Markdown).extractPdfUrls(text));
|
|
|
|
urls = urls.concat(this.lib_(MarkupLanguage.Html).extractPdfUrls(text));
|
|
|
|
} else {
|
|
|
|
urls = this.lib_(language).extractPdfUrls(text);
|
|
|
|
}
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
|
2019-12-29 19:58:40 +02:00
|
|
|
// Create a new MarkupToHtml instance while injecting options specific to Joplin
|
|
|
|
// desktop and mobile applications.
|
2021-05-19 15:00:16 +02:00
|
|
|
public newMarkupToHtml(_plugins: PluginStates = null, options: Options = null) {
|
2019-12-29 19:58:40 +02:00
|
|
|
const subValues = Setting.subValues('markdown.plugin', Setting.toPlainObject());
|
2020-11-12 21:13:28 +02:00
|
|
|
const pluginOptions: any = {};
|
2019-12-29 19:58:40 +02:00
|
|
|
for (const n in subValues) {
|
|
|
|
pluginOptions[n] = { enabled: subValues[n] };
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
options = { ResourceModel: Resource,
|
2019-12-29 19:58:40 +02:00
|
|
|
pluginOptions: pluginOptions,
|
2020-03-10 01:24:57 +02:00
|
|
|
tempDir: Setting.value('tempDir'),
|
|
|
|
fsDriver: shim.fsDriver(),
|
2023-06-01 13:02:36 +02:00
|
|
|
isSafeMode: Setting.value('isSafeMode'), ...options };
|
2019-12-29 19:58:40 +02:00
|
|
|
|
|
|
|
return new MarkupToHtml(options);
|
|
|
|
}
|
2019-07-14 17:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const markupLanguageUtils = new MarkupLanguageUtils();
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
export default markupLanguageUtils;
|