Pending changes exported from your codespace

This commit is contained in:
samuele-cozzi 2023-03-01 23:23:47 +00:00
parent db87eccfda
commit 20db0d46b8
4 changed files with 129 additions and 121 deletions

View file

@ -4,6 +4,7 @@ import { MARP_PREVIEW_VIEW, MarpPreviewView } from './views/marpPreviewView';
import { MarpExport } from './utilities/marpExport';
import { ICON_SLIDE_PREVIEW, ICON_SLIDE_SHOW } from './utilities/icons';
import { MarpSlidesSettings, MarpSlidesSettingTab, DEFAULT_SETTINGS } from 'utilities/settings';
import { FilePath } from 'utilities/filePath';
export default class MarpSlides extends Plugin {
@ -43,56 +44,31 @@ export default class MarpSlides extends Plugin {
this.addCommand({
id: 'marp-export-pdf',
name: 'Export PDF',
callback: async () => {
const file = this.getCurrentFilePath();
const marpCli = new MarpExport(this.settings);
await marpCli.export(file,'pdf');
}
callback: (() => this.exportFile('pdf'))
});
this.addCommand({
id: 'marp-export-pdf-notes',
name: 'Export PDF with Notes',
callback: async () => {
const file = this.getCurrentFilePath();
const marpCli = new MarpExport(this.settings);
await marpCli.export(file,'pdf-with-notes');
}
callback: (() => this.exportFile('pdf-with-notes'))
});
this.addCommand({
id: 'marp-export-html',
name: 'Export HTML',
callback: async () => {
const file = this.getCurrentFilePath();
const marpCli = new MarpExport(this.settings);
await marpCli.export(file,'html');
}
callback: (() => this.exportFile('html'))
});
this.addCommand({
id: 'marp-export-pptx',
name: 'Export PPTX',
callback: async () => {
const file = this.getCurrentFilePath();
const marpCli = new MarpExport(this.settings);
await marpCli.export(file,'pptx');
}
callback: (() => this.exportFile('pptx'))
});
this.addCommand({
id: 'marp-export-png',
name: 'Export PNG',
callback: async () => {
const file = this.getCurrentFilePath();
const marpCli = new MarpExport(this.settings);
await marpCli.export(file,'png');
}
callback: (() => this.exportFile('png'))
});
// This adds an editor command that can perform some operation on the current editor instance
@ -143,6 +119,17 @@ export default class MarpSlides extends Plugin {
}
}
async exportFile(type: string){
const file = this.app.workspace.getActiveFile();
if(file !== null){
const marpCli = new MarpExport(this.settings);
await marpCli.export(
(new FilePath()).getRootPath(file)
,(new FilePath()).getFilePath(file)
,type);
}
}
async showPreviewSlide(){
this.editorView = this.app.workspace.getActiveViewOfType(MarkdownView);
@ -168,17 +155,5 @@ export default class MarpSlides extends Plugin {
return leaf.view as MarpPreviewView;
}
private getCurrentFilePath() {
const file = this.app.workspace.getActiveFile();
const basePath = (file?.vault.adapter as FileSystemAdapter).getBasePath();
console.log(basePath);
console.log(file);
const filePath = `${basePath}\\${file?.path.replace(/\//g,"\\")}`;
console.log(filePath);
return normalizePath(filePath);
}
}

30
src/utilities/filePath.ts Normal file
View file

@ -0,0 +1,30 @@
import { App, PluginSettingTab, Setting, normalizePath, FileSystemAdapter, TFile } from 'obsidian';
import MarpSlides from '../main';
export class FilePath {
getRootPath(file: TFile): string {
const basePath = normalizePath((file.vault.adapter as FileSystemAdapter).getBasePath());
console.log(`Root Path: ${basePath}`);
return basePath;
}
getCompleteFileBasePath(file: TFile){
const basePath = `${this.getRootPath(file)}\\${file.parent.path}\\`;
console.log(`Complete File Base Path: ${basePath}`);
return `${normalizePath(basePath)}`;
}
getCompleteFilePath(file: TFile){
const basePath = `${this.getRootPath(file)}\\${file.path}`;
console.log(`Complete File Path: ${basePath}`);
return `${normalizePath(basePath)}`;
}
getFilePath(file: TFile){
const basePath = `${file.path}`;
console.log(`File Path: ${basePath}`);
return `${normalizePath(basePath)}`;
}
}

View file

@ -6,43 +6,20 @@ export class MarpCLIError extends Error {}
export class MarpExport {
private settings : MarpSlidesSettings;
constructor(settings: MarpSlidesSettings) {
const { CHROME_PATH } = process.env;
try {
process.env.CHROME_PATH = settings.CHROME_PATH || CHROME_PATH;
} catch (e) {
console.error(e)
if (
e instanceof CLIError &&
e.errorCode === CLIErrorCode.NOT_FOUND_CHROMIUM
) {
const browsers = ['[Google Chrome](https://www.google.com/chrome/)']
if (process.platform === 'linux')
browsers.push('[Chromium](https://www.chromium.org/)')
browsers.push('[Microsoft Edge](https://www.microsoft.com/edge)')
throw new MarpCLIError(
`It requires to install ${browsers
.join(', ')
.replace(/, ([^,]*)$/, ' or $1')} for exporting.`
)
}
throw e
} finally {
//process.env.CHROME_PATH = CHROME_PATH
}
this.settings = settings;
}
async export(filePath: string | undefined, type: string){
console.log(filePath);
async export(rootPath: string, filePath: string | undefined, type: string){
if (filePath !== undefined){
const argv: string[] = [filePath,'--allow-local-files', '--theme-set', normalizePath('C:\\Users\\samue\\code\\knowledge-base\\templates\\marp\\themes')];
const themePath = normalizePath(`${rootPath}\\${this.settings.ThemePath}`);
const completeFilePath = normalizePath(`${rootPath}\\${filePath}`);
console.log(completeFilePath);
console.log(themePath);
//const argv: string[] = [filePath,'--allow-local-files', '--theme-set', normalizePath('C:\\Users\\samue\\code\\knowledge-base\\templates\\marp\\themes')];
const argv: string[] = [completeFilePath,'--allow-local-files', '--theme-set', themePath];
switch (type) {
case 'pdf':
argv.push('--pdf');
@ -66,40 +43,65 @@ export class MarpExport {
//argv.push('--engine');
//argv.push('@marp-team/marpit');
}
await this.runMarpCli(argv);
await this.run(argv);
}
}
//async exportPdf(argv: string[], opts?: MarpCLIAPIOptions | undefined){
private async runMarpCli(argv: string[]){
private async run(argv: string[]){
const { CHROME_PATH } = process.env;
try {
process.env.CHROME_PATH = this.settings.CHROME_PATH || CHROME_PATH;
this.runMarpCli(argv);
} catch (e) {
console.error(e)
if (
e instanceof CLIError &&
e.errorCode === CLIErrorCode.NOT_FOUND_CHROMIUM
) {
const browsers = ['[Google Chrome](https://www.google.com/chrome/)']
if (process.platform === 'linux')
browsers.push('[Chromium](https://www.chromium.org/)')
browsers.push('[Microsoft Edge](https://www.microsoft.com/edge)')
throw new MarpCLIError(
`It requires to install ${browsers
.join(', ')
.replace(/, ([^,]*)$/, ' or $1')} for exporting.`
)
}
throw e
} finally {
process.env.CHROME_PATH = CHROME_PATH
}
}
private async runMarpCli(argv: string[]) {
//console.info(`Execute Marp CLI [${argv.join(' ')}] (${JSON.stringify(opts)})`)
console.info(`Execute Marp CLI [${argv.join(' ')}]`);
console.log(process.env.CHROME_PATH);
// const { marpCli } = await import(
// '@marp-team/marp-cli'
// )
//exitCode = await marpCli(argv, opts)
try {
const exitCode = await marpCli(argv)
marpCli(argv)
.then((exitStatus) => {
if (exitStatus > 0) {
console.error(`Failure (Exit status: ${exitStatus})`)
} else {
if (exitCode > 0) {
console.error(`Failure (Exit status: ${exitCode})`)
} else {
console.log('Success')
}
})
.catch((e) =>{
if (e instanceof CLIError){
console.log("Errore!");
console.log(e.message);
console.log(e.errorCode);
} else {
console.log("Errore");
}
});
}
} catch(e) {
if (e instanceof CLIError){
console.log(`CLIError code: ${e.errorCode}, message: ${e.message}`);
} else {
console.log("Generic Error!");
}
}
}
}

View file

@ -2,6 +2,7 @@ import { ItemView, WorkspaceLeaf, TFile, MarkdownView, normalizePath, FileSystem
import { Marp } from '@marp-team/marp-core'
import { MarpSlidesSettings } from '../utilities/settings'
import { FilePath } from '../utilities/filePath'
export const MARP_PREVIEW_VIEW = 'marp-preview-view';
@ -48,7 +49,7 @@ export class MarpPreviewView extends ItemView {
async displaySlides(view : MarkdownView) {
console.log("Marp Preview Display Slides");
const basePath = this.getCurrentFileBasePath(view.file);
const basePath = `app://local/${(new FilePath).getCompleteFileBasePath(view.file)}/`;
const markdownText = view.data;
const container = this.containerEl.children[1];
@ -73,31 +74,31 @@ export class MarpPreviewView extends ItemView {
container.innerHTML = htmlFile;
}
getCurrentFileBasePath(file: TFile){
// const resourcePath = this.app.vault.adapter.getResourcePath(file.parent.path);
// getCurrentFileBasePath(file: TFile){
// // const resourcePath = this.app.vault.adapter.getResourcePath(file.parent.path);
// let basePath = '';
// if(file.parent.isRoot()){
// basePath = `${resourcePath?.substring(0, resourcePath.indexOf("?"))}`;
// }
// else
// {
// basePath = `${resourcePath?.substring(0, resourcePath.indexOf("?"))}/`;
// }
// // let basePath = '';
// // if(file.parent.isRoot()){
// // basePath = `${resourcePath?.substring(0, resourcePath.indexOf("?"))}`;
// // }
// // else
// // {
// // basePath = `${resourcePath?.substring(0, resourcePath.indexOf("?"))}/`;
// // }
const basePath1 = `${(file?.vault.adapter as FileSystemAdapter).getBasePath()}\\${file.parent.path}\\`;
// const basePath1 = `${(file?.vault.adapter as FileSystemAdapter).getBasePath()}\\${file.parent.path}\\`;
console.log(file);
// console.log(basePath);
// console.log(`${normalizePath(basePath)}/`);
console.log(basePath1);
console.log(`${normalizePath(basePath1)}/`);
// console.log(file);
// // console.log(basePath);
// // console.log(`${normalizePath(basePath)}/`);
// console.log(basePath1);
// console.log(`${normalizePath(basePath1)}/`);
//app://local/C:/Users/samue/code/knowledge-base/bookshelf/tech_management/
//app://local/C:/Users/samue/code/knowledge-base/bookshelf/tech_management/
//return basePath;
return `app://local/${normalizePath(basePath1)}/`;
}
// //app://local/C:/Users/samue/code/knowledge-base/bookshelf/tech_management/
// //app://local/C:/Users/samue/code/knowledge-base/bookshelf/tech_management/
// //return basePath;
// return `app://local/${normalizePath(basePath1)}/`;
// }
}