Skip to content

Commit

Permalink
Refactoring: Stronger types, move userMarkupToHtml to the gui/hooks
Browse files Browse the repository at this point in the history
directory
  • Loading branch information
personalizedrefrigerator committed Jan 11, 2025
1 parent 91dc830 commit 73274ec
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ packages/app-desktop/gui/NoteEditor/utils/useEffectiveNoteId.js
packages/app-desktop/gui/NoteEditor/utils/useFolder.js
packages/app-desktop/gui/NoteEditor/utils/useFormNote.test.js
packages/app-desktop/gui/NoteEditor/utils/useFormNote.js
packages/app-desktop/gui/NoteEditor/utils/useMarkupToHtml.js
packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.js
packages/app-desktop/gui/NoteEditor/utils/useNoteSearchBar.js
packages/app-desktop/gui/NoteEditor/utils/usePluginEditorView.test.js
Expand Down Expand Up @@ -480,6 +479,7 @@ packages/app-desktop/gui/hooks/useDocument.js
packages/app-desktop/gui/hooks/useEffectDebugger.js
packages/app-desktop/gui/hooks/useElementHeight.js
packages/app-desktop/gui/hooks/useImperativeHandlerDebugger.js
packages/app-desktop/gui/hooks/useMarkupToHtml.js
packages/app-desktop/gui/hooks/usePrevious.js
packages/app-desktop/gui/hooks/usePropsDebugger.js
packages/app-desktop/gui/lib/SearchInput/SearchInput.js
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ packages/app-desktop/gui/NoteEditor/utils/useEffectiveNoteId.js
packages/app-desktop/gui/NoteEditor/utils/useFolder.js
packages/app-desktop/gui/NoteEditor/utils/useFormNote.test.js
packages/app-desktop/gui/NoteEditor/utils/useFormNote.js
packages/app-desktop/gui/NoteEditor/utils/useMarkupToHtml.js
packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.js
packages/app-desktop/gui/NoteEditor/utils/useNoteSearchBar.js
packages/app-desktop/gui/NoteEditor/utils/usePluginEditorView.test.js
Expand Down Expand Up @@ -455,6 +454,7 @@ packages/app-desktop/gui/hooks/useDocument.js
packages/app-desktop/gui/hooks/useEffectDebugger.js
packages/app-desktop/gui/hooks/useElementHeight.js
packages/app-desktop/gui/hooks/useImperativeHandlerDebugger.js
packages/app-desktop/gui/hooks/useMarkupToHtml.js
packages/app-desktop/gui/hooks/usePrevious.js
packages/app-desktop/gui/hooks/usePropsDebugger.js
packages/app-desktop/gui/lib/SearchInput/SearchInput.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { useState, useEffect, useRef, forwardRef, useCallback, useImperativeHandle, ForwardedRef, useContext } from 'react';

// eslint-disable-next-line no-unused-vars
import { EditorCommand, MarkupToHtmlOptions, NoteBodyEditorProps, NoteBodyEditorRef } from '../../../utils/types';
import { EditorCommand, NoteBodyEditorProps, NoteBodyEditorRef } from '../../../utils/types';
import { commandAttachFileToBody, getResourcesFromPasteEvent } from '../../../utils/resourceHandling';
import { ScrollOptions, ScrollOptionTypes } from '../../../utils/types';
import { CommandValue } from '../../../utils/types';
Expand Down Expand Up @@ -34,6 +34,7 @@ import useWebviewIpcMessage from '../utils/useWebviewIpcMessage';
import useEditorSearchHandler from '../utils/useEditorSearchHandler';
import { focus } from '@joplin/lib/utils/focusHandler';
import { WindowIdContext } from '../../../../NewWindowOrIFrame';
import { MarkupToHtmlOptions } from '../../../../hooks/useMarkupToHtml';

function markupRenderOptions(override: MarkupToHtmlOptions = null): MarkupToHtmlOptions {
return { ...override };
Expand Down
2 changes: 1 addition & 1 deletion packages/app-desktop/gui/NoteEditor/NoteEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useNoteSearchBar from './utils/useNoteSearchBar';
import useMessageHandler from './utils/useMessageHandler';
import useWindowCommandHandler from './utils/useWindowCommandHandler';
import useDropHandler from './utils/useDropHandler';
import useMarkupToHtml from './utils/useMarkupToHtml';
import useMarkupToHtml from '../hooks/useMarkupToHtml';
import useFormNote, { OnLoadEvent, OnSetFormNote } from './utils/useFormNote';
import useEffectiveNoteId from './utils/useEffectiveNoteId';
import useFolder from './utils/useFolder';
Expand Down
19 changes: 2 additions & 17 deletions packages/app-desktop/gui/NoteEditor/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DropHandler } from './useDropHandler';
import { SearchMarkers } from './useSearchMarkers';
import { ParseOptions } from '@joplin/lib/HtmlToMd';
import { ScrollStrategy } from '@joplin/editor/CodeMirror/CodeMirrorControl';
import { MarkupToHtmlOptions } from '../../hooks/useMarkupToHtml';

export interface AllAssetsOptions {
contentMaxWidthTarget?: string;
Expand Down Expand Up @@ -73,23 +74,7 @@ export interface NoteBodyEditorRef {
execCommand(command: CommandValue): Promise<void>;
}

export interface MarkupToHtmlOptions {
replaceResourceInternalToExternalLinks?: boolean;
resourceInfos?: ResourceInfos;
contentMaxWidth?: number;
increaseControlsSize?: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
plugins?: Record<string, any>;
bodyOnly?: boolean;
mapsToLine?: boolean;
useCustomPdfViewer?: boolean;
noteId?: string;
vendorDir?: string;
platformName?: string;
allowedFilePrefixes?: string[];
whiteBackgroundNoteRendering?: boolean;
}

export { MarkupToHtmlOptions };
export type MarkupToHtmlHandler = (markupLanguage: MarkupLanguage, markup: string, options: MarkupToHtmlOptions)=> Promise<RenderResult>;
export type HtmlToMarkdownHandler = (markupLanguage: number, html: string, originalCss: string, parseOptions?: ParseOptions)=> Promise<string>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import shim from '@joplin/lib/shim';

const { themeStyle } = require('@joplin/lib/theme');
import Note from '@joplin/lib/models/Note';
import { MarkupToHtmlOptions, ResourceInfos } from './types';
import { ResourceInfos } from '../NoteEditor/utils/types';
import { resourceFullPath } from '@joplin/lib/models/utils/resourceUtils';
import { RenderOptions } from '@joplin/renderer/types';

export interface MarkupToHtmlOptions extends RenderOptions {
resourceInfos?: ResourceInfos;
replaceResourceInternalToExternalLinks?: boolean;
}

interface HookDependencies {
themeId: number;
Expand All @@ -33,8 +39,7 @@ export default function useMarkupToHtml(deps: HookDependencies) {
});
}, [plugins, customCss, resourceBaseUrl]);

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
return useCallback(async (markupLanguage: number, md: string, options: MarkupToHtmlOptions = null): Promise<any> => {
return useCallback(async (markupLanguage: number, md: string, options: MarkupToHtmlOptions|null = null) => {
options = {
replaceResourceInternalToExternalLinks: false,
resourceInfos: {},
Expand Down
14 changes: 10 additions & 4 deletions packages/lib/services/interop/InteropService_Exporter_Html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Folder from '../../models/Folder';
import Note from '../../models/Note';
import Setting from '../../models/Setting';
import { MarkupToHtml } from '@joplin/renderer';
import { NoteEntity, ResourceEntity } from '../database/types';
import { NoteEntity, ResourceEntity, ResourceLocalStateEntity } from '../database/types';
import { contentScriptsToRendererRules } from '../plugins/utils/loadContentScripts';
import { basename, friendlySafeFilename, rtrimSlashes, dirname } from '../../path-utils';
import htmlpack from '@joplin/htmlpack';
Expand All @@ -17,6 +17,8 @@ import getPluginSettingValue from '../plugins/utils/getPluginSettingValue';
import { LinkRenderingType } from '@joplin/renderer/MdToHtml';
import Logger from '@joplin/utils/Logger';
import { parseRenderedNoteMetadata } from './utils';
import ResourceLocalState from '../../models/ResourceLocalState';
import { ResourceInfos } from '@joplin/renderer/types';

const logger = Logger.create('InteropService_Exporter_Html');

Expand All @@ -28,7 +30,7 @@ export default class InteropService_Exporter_Html extends InteropService_Exporte
private createdDirs_: string[] = [];
private resourceDir_: string;
private markupToHtml_: MarkupToHtml;
private resources_: ResourceEntity[] = [];
private resources_: ResourceInfos = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
private style_: any;
private packIntoSingleFile_ = false;
Expand Down Expand Up @@ -174,10 +176,14 @@ export default class InteropService_Exporter_Html extends InteropService_Exporte
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
public async processResource(resource: any, filePath: string) {
public async processResource(resource: ResourceEntity, filePath: string) {
const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`;
await shim.fsDriver().copy(filePath, destResourcePath);
this.resources_.push(resource);
const localState: ResourceLocalStateEntity = await ResourceLocalState.load(resource.id);
this.resources_[resource.id] = {
localState,
item: resource,
};
}

public async close() {
Expand Down
5 changes: 2 additions & 3 deletions packages/renderer/MdToHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fileExtension } from '@joplin/utils/path';
import setupLinkify from './MdToHtml/setupLinkify';
import validateLinks from './MdToHtml/validateLinks';
import { Options as NoteStyleOptions } from './noteStyle';
import { FsDriver, ItemIdToUrlHandler, MarkupRenderer, OptionsResourceModel, RenderOptions, RenderResult, RenderResultPluginAsset } from './types';
import { FsDriver, ItemIdToUrlHandler, MarkupRenderer, OptionsResourceModel, RenderOptions, RenderResult, RenderResultPluginAsset, ResourceInfos } from './types';
import hljs from './highlight';
import * as MarkdownIt from 'markdown-it';

Expand Down Expand Up @@ -161,8 +161,7 @@ export interface RuleOptions {
postMessageSyntax: string;
ResourceModel: OptionsResourceModel;
resourceBaseUrl: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
resources: any; // resourceId: Resource
resources: ResourceInfos; // resourceId: Resource

// Used by checkboxes to specify how it should be rendered
checkboxRenderingType?: number;
Expand Down
6 changes: 3 additions & 3 deletions packages/renderer/MdToHtml/linkReplacement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('linkReplacement', () => {
ResourceModel: defaultResourceModel,
resources: {
[resourceId]: {
item: {},
item: { id: 'test' },
localState: {
fetch_status: 2, // FETCH_STATUS_DONE
},
Expand All @@ -46,7 +46,7 @@ describe('linkReplacement', () => {
ResourceModel: defaultResourceModel,
resources: {
[resourceId]: {
item: {},
item: { id: 'test' },
localState: {
fetch_status: 0, // FETCH_STATUS_IDLE
},
Expand All @@ -66,7 +66,7 @@ describe('linkReplacement', () => {
ResourceModel: defaultResourceModel,
resources: {
[resourceId]: {
item: {},
item: { id: 'test' },
localState: {
fetch_status: 2, // FETCH_STATUS_DONE
},
Expand Down
5 changes: 2 additions & 3 deletions packages/renderer/MdToHtml/linkReplacement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LinkRenderingType } from '../MdToHtml';
import { ItemIdToUrlHandler, OptionsResourceModel } from '../types';
import { ItemIdToUrlHandler, OptionsResourceModel, ResourceInfos } from '../types';
import * as utils from '../utils';
import createEventHandlingAttrs from './createEventHandlingAttrs';
const Entities = require('html-entities').AllHtmlEntities;
Expand All @@ -9,8 +9,7 @@ const { getClassNameForMimeType } = require('font-awesome-filetypes');

export interface Options {
title?: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
resources?: any;
resources?: ResourceInfos;
ResourceModel?: OptionsResourceModel;
linkRenderingType?: LinkRenderingType;
plainResourceRendering?: boolean;
Expand Down
20 changes: 16 additions & 4 deletions packages/renderer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import { Options as NoteStyleOptions } from './noteStyle';
export type ItemIdToUrlHandler = (resourceId: string, urlParameters?: string)=> string;

interface ResourceEntity {
id: string;
id?: string;
title?: string;
mime?: string;
file_extension?: string;
updated_time?: number;

encryption_applied?: number;
encryption_blob_encrypted?: number;
}

interface ResourceLocalState {
fetch_status?: number;
}

interface ResourceInfo {
localState: unknown;
export interface ResourceInfo {
localState: ResourceLocalState;
item: ResourceEntity;
}

export type ResourceInfos = Record<string, ResourceInfo>;

export interface FsDriver {
writeFile: (path: string, content: string, encoding: string)=> Promise<void>;
exists: (path: string)=> Promise<boolean>;
Expand Down Expand Up @@ -54,13 +64,15 @@ export interface RenderOptions {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
settingValue?: (pluginId: string, key: string)=> any;

resources?: Record<string, ResourceInfo>;
resources?: ResourceInfos;

onResourceLoaded?: ()=> void;
editPopupFiletypes?: string[];
createEditPopupSyntax?: string;
destroyEditPopupSyntax?: string;

platformName?: string;

// HtmlToHtml only
whiteBackgroundNoteRendering?: boolean;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/renderer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { attributesHtml } from './htmlUtils';
import { ItemIdToUrlHandler, OptionsResourceModel } from './types';
import { ItemIdToUrlHandler, OptionsResourceModel, ResourceInfo, ResourceInfos } from './types';

const Entities = require('html-entities').AllHtmlEntities;
const htmlentities = new Entities().encode;
Expand Down Expand Up @@ -98,8 +98,7 @@ export const resourceStatusName = function(index: number) {
throw new Error(`Unknown index: ${index}`);
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
export const resourceStatus = function(ResourceModel: OptionsResourceModel, resourceInfo: any) {
export const resourceStatus = function(ResourceModel: OptionsResourceModel, resourceInfo: ResourceInfo) {
if (!ResourceModel) return 'ready';

let status = 'ready';
Expand Down Expand Up @@ -130,8 +129,7 @@ type ImageMarkupData = {
title: string;
}|{ src: string; before: string; after: string };

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
export const imageReplacement = function(ResourceModel: OptionsResourceModel, markup: ImageMarkupData, resources: any, resourceBaseUrl: string, itemIdToUrl: ItemIdToUrlHandler = null) {
export const imageReplacement = function(ResourceModel: OptionsResourceModel, markup: ImageMarkupData, resources: ResourceInfos, resourceBaseUrl: string, itemIdToUrl: ItemIdToUrlHandler = null) {
if (!ResourceModel || !resources) return null;

const src = markup.src;
Expand Down

0 comments on commit 73274ec

Please sign in to comment.