fix: ExportWholeWikiHTML as index.html

This commit is contained in:
lin onetwo 2023-05-22 21:31:54 +08:00
parent 6f0179a90e
commit 7cc3ab0d4d
9 changed files with 38 additions and 16 deletions

View file

@ -409,7 +409,7 @@
"PrintPage": "Print Page",
"PrintActiveTiddler": "Print Active Tiddler",
"Wiki": "Wiki",
"ExportWholeWikiHTML": "Export Whole Wiki as HTML"
"ExportWholeWikiHTML": "Export Whole Wiki as HTML to folder"
},
"ErrorMessage": "Error message",
"ClickForDetails": "Click For Details",

View file

@ -103,7 +103,7 @@
"PrintPage": "打印页面",
"PrintActiveTiddler": "打印当前笔记",
"Wiki": "Wiki",
"ExportWholeWikiHTML": "导出整个Wiki为HTML"
"ExportWholeWikiHTML": "导出整个Wiki为HTML存入文件夹"
},
"AddWorkspace": {
"AddWorkspace": "添加工作区",

View file

@ -4,3 +4,8 @@ export const developmentHttpsCertKeyFolderName = 'https-keys-dev';
/** Used to place mock wiki during dev and testing */
export const developmentWikiFolderName = 'tidgi-dev';
export const localizationFolderName = 'localization';
export const wikiPictureExtensions = ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'tif', 'bmp', 'dib'];
export const wikiHtmlExtensions = ['html', 'htm', 'hta', 'Html', 'HTML', 'HTM', 'HTA'];
export const tlsCertExtensions = ['crt'];
export const tlsKeyExtensions = ['key'];

View file

@ -7,6 +7,7 @@ import styled from 'styled-components';
import { ListItem, ListItemText } from '@/components/ListItem';
import { rootTiddlers } from '@/constants/defaultTiddlerNames';
import { tlsCertExtensions, tlsKeyExtensions } from '@/constants/fileNames';
import { defaultServerIP } from '@/constants/urls';
import { IWorkspace } from '@services/workspaces/interface';
@ -26,9 +27,6 @@ const AutocompleteWithMarginTop: typeof Autocomplete = styled(Autocomplete)`
margin-top: 8px;
`;
const tlsCertExtensions = ['crt'];
const tlsKeyExtensions = ['key'];
export interface IBlogOptionsProps {
actualIP: string | undefined;
workspace: IWorkspace;

View file

@ -21,6 +21,7 @@ import { List, ListItem, ListItemText } from '@/components/ListItem';
import { useRestartSnackbar } from '@/components/RestartSnackbar';
import { TokenForm } from '@/components/TokenForm';
import { DEFAULT_USER_NAME, getTidGiAuthHeaderWithToken } from '@/constants/auth';
import { wikiPictureExtensions } from '@/constants/fileNames';
import { useActualIp } from '@services/native/hooks';
import { SupportedStorageServices } from '@services/types';
import { isEqual } from 'lodash';
@ -139,7 +140,6 @@ const getValidIconPath = (iconPath?: string | null): string => {
};
const workspaceID = (window.meta as WindowMeta[WindowNames.editWorkspace]).workspaceID as string;
const wikiPictureExtensions = ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'tif', 'bmp', 'dib'];
export default function EditWorkspace(): JSX.Element {
const { t } = useTranslation();

View file

@ -19,7 +19,7 @@ import { IWorkspaceService } from '@services/workspaces/interface';
import i18next from 'i18next';
import { ZxNotInitializedError } from './error';
import { findEditorOrDefault, findGitGUIAppOrDefault, launchExternalEditor } from './externalApp';
import { INativeService } from './interface';
import { INativeService, IPickDirectoryOptions } from './interface';
import { reportErrorToGithubWithTemplates } from './reportError';
@injectable()
@ -176,10 +176,11 @@ ${message.message}
}
}
public async pickDirectory(defaultPath?: string): Promise<string[]> {
public async pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise<string[]> {
const dialogResult = await dialog.showOpenDialog({
properties: ['openDirectory'],
properties: options?.allowOpenFile === true ? ['openDirectory', 'openFile'] : ['openDirectory'],
defaultPath,
filters: options?.filters,
});
if (!dialogResult.canceled && dialogResult.filePaths.length > 0) {
return dialogResult.filePaths;

View file

@ -6,6 +6,14 @@ import { IZxFileInput } from '@services/wiki/wikiWorker';
import { WindowNames } from '@services/windows/WindowProperties';
import { ProxyPropertyType } from 'electron-ipc-cat/common';
export interface IPickDirectoryOptions {
/**
* Only works in MacOS, will use openDirectory as default on other platforms
* @url https://github.com/electron/electron/issues/26885
*/
allowOpenFile?: boolean;
filters?: Electron.OpenDialogOptions['filters'];
}
/**
* Wrap call to electron api, so we won't need remote module in renderer process
*/
@ -33,7 +41,7 @@ export interface INativeService {
openNewGitHubIssue(error: Error): Promise<void>;
openPath(filePath: string): Promise<void>;
path(method: 'basename' | 'dirname' | 'join', pathString: string | undefined, ...paths: string[]): Promise<string | undefined>;
pickDirectory(defaultPath?: string): Promise<string[]>;
pickDirectory(defaultPath?: string, options?: IPickDirectoryOptions): Promise<string[]>;
pickFile(filters?: Electron.OpenDialogOptions['filters']): Promise<string[]>;
quit(): void;
/**

View file

@ -196,14 +196,19 @@ function executeZxScript(file: IZxFileInput, zxPath: string): Observable<IZxWork
});
}
/**
* wikiHtmlExtensions
*/
const isHtmlWikiRegex = /(?:html|htm|Html|HTML|HTM|HTA|hta)$/;
const isHtmlWiki = (htmlWikiPath: string) => isHtmlWikiRegex.test(htmlWikiPath);
async function extractWikiHTML(htmlWikiPath: string, saveWikiFolderPath: string, constants: { TIDDLYWIKI_PACKAGE_FOLDER: string }): Promise<void> {
// tiddlywiki --load ./mywiki.html --savewikifolder ./mywikifolder
// --savewikifolder <wikifolderpath> [<filter>]
// . /mywikifolder is the path where the tiddlder and plugins folders are stored
const { TIDDLYWIKI_PACKAGE_FOLDER } = constants;
const reg = /(?:html|htm|Html|HTML|HTM|HTA|hta)$/;
const isHtmlWiki = reg.test(htmlWikiPath);
if (!isHtmlWiki) {
if (!isHtmlWiki(htmlWikiPath)) {
throw new Error(`Please enter the path to the tiddlywiki.html file. Current path can't be used. ${htmlWikiPath}`);
}
if (await exists(saveWikiFolderPath)) {
@ -226,12 +231,13 @@ async function extractWikiHTML(htmlWikiPath: string, saveWikiFolderPath: string,
});
}
async function packetHTMLFromWikiFolder(folderWikiPath: string, folderToSaveWikiHtml: string, constants: { TIDDLYWIKI_PACKAGE_FOLDER: string }): Promise<void> {
async function packetHTMLFromWikiFolder(folderWikiPath: string, pathOfNewHTML: string, constants: { TIDDLYWIKI_PACKAGE_FOLDER: string }): Promise<void> {
// tiddlywiki ./mywikifolder --rendertiddler '$:/core/save/all' mywiki.html text/plain
// . /mywikifolder is the path to the wiki folder, which generally contains the tiddlder and plugins directories
const { TIDDLYWIKI_PACKAGE_FOLDER } = constants;
const wikiInstance = TiddlyWiki();
wikiInstance.boot.argv = [folderWikiPath, '--rendertiddler', '$:/core/save/all', folderToSaveWikiHtml, 'text/plain'];
// a .html file path should be provided, but if provided a folder path, we can add /index.html to fix it.
wikiInstance.boot.argv = [folderWikiPath, '--rendertiddler', '$:/core/save/all', isHtmlWiki(pathOfNewHTML) ? pathOfNewHTML : `${pathOfNewHTML}/index.html`, 'text/plain'];
await new Promise<void>((resolve, reject) => {
try {
wikiInstance.boot.startup({

View file

@ -7,6 +7,7 @@ import { injectable } from 'inversify';
import { DEFAULT_DOWNLOADS_PATH } from '@/constants/appPaths';
import { MetaDataChannel, WikiChannel } from '@/constants/channels';
import { wikiHtmlExtensions } from '@/constants/fileNames';
import { tiddlywikiLanguagesMap } from '@/constants/languages';
import { WikiCreationMethod } from '@/constants/wikiCreation';
import type { IAuthenticationService } from '@services/auth/interface';
@ -269,7 +270,10 @@ export class WorkspaceView implements IWorkspaceViewService {
logger.error('Can not export whole wiki, activeWorkspace is undefined');
return;
}
const folderToSaveWikiHtml = await this.nativeService.pickDirectory(DEFAULT_DOWNLOADS_PATH);
const folderToSaveWikiHtml = await this.nativeService.pickDirectory(DEFAULT_DOWNLOADS_PATH, {
allowOpenFile: true,
filters: [{ name: 'HTML', extensions: wikiHtmlExtensions }],
});
await this.wikiService.packetHTMLFromWikiFolder(activeWorkspace.wikiFolderLocation, folderToSaveWikiHtml[0]);
},
enabled: hasWorkspaces,