mirror of
https://github.com/samuele-cozzi/obsidian-marp-slides.git
synced 2026-07-22 07:40:25 +00:00
export pdf
This commit is contained in:
parent
a288fe2508
commit
8644d19c3f
7 changed files with 85432 additions and 2768 deletions
|
|
@ -20,6 +20,8 @@ const context = await esbuild.context({
|
|||
external: [
|
||||
"obsidian",
|
||||
"electron",
|
||||
"chromium-bidi/lib/cjs/bidiMapper/bidiMapper.js",
|
||||
"emitter",
|
||||
"@codemirror/autocomplete",
|
||||
"@codemirror/collab",
|
||||
"@codemirror/commands",
|
||||
|
|
@ -30,7 +32,7 @@ const context = await esbuild.context({
|
|||
"@codemirror/view",
|
||||
"@lezer/common",
|
||||
"@lezer/highlight",
|
||||
"@lezer/lr",
|
||||
"@lezer/lr",
|
||||
...builtins],
|
||||
format: "cjs",
|
||||
target: "es2018",
|
||||
|
|
|
|||
85183
main.js
85183
main.js
File diff suppressed because one or more lines are too long
2822
package-lock.json
generated
2822
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "obsidian-marp-slides",
|
||||
"version": "1.0.0",
|
||||
"description": "Create markdown-based reveal.js presentations in Obsidian ",
|
||||
"description": "Create markdown-based marp presentations in Obsidian ",
|
||||
"main": "src/main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
|
|
@ -22,6 +22,7 @@
|
|||
"typescript": "4.7.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@marp-team/marp-cli": "^2.4.0",
|
||||
"@marp-team/marp-core": "^3.5.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
79
src/main.ts
79
src/main.ts
|
|
@ -1,6 +1,7 @@
|
|||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
||||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, FileSystemAdapter, Setting } from 'obsidian';
|
||||
|
||||
import { MARP_PREVIEW_VIEW, MarpPreviewView } from './views/marpPreviewView';
|
||||
import { MarpExport } from './utilities/marpExport';
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
|
|
@ -39,18 +40,43 @@ export default class MarpSlides extends Plugin {
|
|||
// // Perform additional things with the ribbon
|
||||
//ribbonIconEl.addClass('my-plugin-ribbon-class');
|
||||
|
||||
this.addCommand({
|
||||
id: 'marp-slides-preview',
|
||||
name: 'Slide Preview',
|
||||
callback: () => {
|
||||
this.showView();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
this.addCommand({
|
||||
id: 'marp-slides-preview',
|
||||
name: 'Slide Preview',
|
||||
callback: () => {
|
||||
this.showView();
|
||||
}
|
||||
});
|
||||
|
||||
// This adds a simple command that can be triggered anywhere
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-simple',
|
||||
name: 'Open sample modal (simple)',
|
||||
callback: () => {
|
||||
this.showView();
|
||||
id: 'marp-export-pdf',
|
||||
name: 'Export PDF',
|
||||
callback: async () => {
|
||||
console.log("export pdf - start");
|
||||
const file = this.app.workspace.getActiveFile();
|
||||
const basePath = (file?.vault.adapter as FileSystemAdapter).getBasePath();
|
||||
console.log(basePath);
|
||||
console.log(file);
|
||||
|
||||
|
||||
const marpCli = new MarpExport();
|
||||
//let filepath = basePath + file?.name;
|
||||
//await marpCli.exportPdf("C:\\Users\\samue\\code\\knowledge-base\\CICDSlides1.md");
|
||||
await marpCli.exportPdf(`${basePath}\\${file?.path.replace(/\//g,"\\")}`);
|
||||
//new MarpSlidesModal(this.app).open();
|
||||
console.log("export pdf - end");
|
||||
}
|
||||
});
|
||||
|
||||
// This adds an editor command that can perform some operation on the current editor instance
|
||||
this.addCommand({
|
||||
id: 'sample-editor-command',
|
||||
|
|
@ -60,25 +86,7 @@ export default class MarpSlides extends Plugin {
|
|||
editor.replaceSelection('Sample Editor Command');
|
||||
}
|
||||
});
|
||||
// This adds a complex command that can check whether the current state of the app allows execution of the command
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-complex',
|
||||
name: 'Open sample modal (complex)',
|
||||
checkCallback: (checking: boolean) => {
|
||||
// Conditions to check
|
||||
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (markdownView) {
|
||||
// If checking is true, we're simply "checking" if the command can be run.
|
||||
// If checking is false, then we want to actually perform the operation.
|
||||
if (!checking) {
|
||||
new MarpSlidesModal(this.app).open();
|
||||
}
|
||||
|
||||
// This command will only show up in Command Palette when the check function returns true
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new MarpSlidesSettingTab(this.app, this));
|
||||
|
|
@ -130,16 +138,21 @@ export default class MarpSlides extends Plugin {
|
|||
|
||||
const instance = await this.activateView();
|
||||
instance.displaySlides(basePath, this.markdownViewText);
|
||||
|
||||
// const url = this.revealServer.getUrl();
|
||||
// url.pathname = this.fixedEncodeURIComponent(this.target.path);
|
||||
|
||||
// this.openUrl(url);
|
||||
// this.showMotm();
|
||||
|
||||
//const instance = this.getViewInstance();
|
||||
}
|
||||
|
||||
getCurrentFileBasePath(){
|
||||
const file = this.app.workspace.getActiveFile();
|
||||
console.log(file);
|
||||
const resourcePath = file?.vault.adapter.getResourcePath("");
|
||||
const rootPath = resourcePath?.substring(0, resourcePath.indexOf("?"))
|
||||
const basePath = `${rootPath}/${file?.parent.path}/`;
|
||||
|
||||
return basePath;
|
||||
}
|
||||
|
||||
async exportFile(){
|
||||
|
||||
}
|
||||
// async activateView() {
|
||||
// this.app.workspace.detachLeavesOfType(MARP_PREVIEW_VIEW);
|
||||
|
||||
|
|
|
|||
76
src/utilities/marpExport.ts
Normal file
76
src/utilities/marpExport.ts
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
//import { ItemView, MarkdownView, Menu, WorkspaceLeaf } from 'obsidian';
|
||||
import { Marp } from '@marp-team/marp-core'
|
||||
import type { marpCli } from '@marp-team/marp-cli'
|
||||
|
||||
export class MarpCLIError extends Error {}
|
||||
|
||||
export class MarpExport {
|
||||
|
||||
async exportPdf(filePath: string | undefined) {
|
||||
console.log(filePath);
|
||||
if (filePath !== undefined){
|
||||
let argv: string[] = [filePath, '--pdf','--pdf-notes','--pdf-outlines','--allow-local-files'];
|
||||
await this.runMarpCli(argv);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//async exportPdf(argv: string[], opts?: MarpCLIAPIOptions | undefined){
|
||||
private async runMarpCli(argv: string[]){
|
||||
|
||||
//console.info(`Execute Marp CLI [${argv.join(' ')}] (${JSON.stringify(opts)})`)
|
||||
console.info(`Execute Marp CLI [${argv.join(' ')}]`);
|
||||
|
||||
const { marpCli, CLIError, CLIErrorCode } = await import(
|
||||
'@marp-team/marp-cli'
|
||||
)
|
||||
const { CHROME_PATH } = process.env
|
||||
|
||||
try {
|
||||
//process.env.CHROME_PATH = marpConfiguration().get<string>('chromePath') || CHROME_PATH
|
||||
process.env.CHROME_PATH = CHROME_PATH
|
||||
|
||||
console.log("start marp cli");
|
||||
//exitCode = await marpCli(argv, opts)
|
||||
marpCli(argv)
|
||||
.then((exitStatus) => {
|
||||
if (exitStatus > 0) {
|
||||
console.error(`Failure (Exit status: ${exitStatus})`)
|
||||
} else {
|
||||
console.log('Success')
|
||||
}
|
||||
})
|
||||
.catch((e) =>{
|
||||
console.log("Errore");
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
console.log("start marp cli");
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -15,19 +15,15 @@ export class MarpPreviewView extends ItemView {
|
|||
}
|
||||
|
||||
getDisplayText() {
|
||||
return "Example view";
|
||||
return "Deck Preview";
|
||||
}
|
||||
|
||||
async onOpen() {
|
||||
console.log("Marp Preview onOpen View");
|
||||
|
||||
// const container = this.containerEl.children[1];
|
||||
// container.empty();
|
||||
// container.createEl("h4", { text: "Example view" });
|
||||
//console.log("Marp Preview onOpen View");
|
||||
}
|
||||
|
||||
async onClose() {
|
||||
console.log("Marp Preview onClose View");
|
||||
//console.log("Marp Preview onClose View");
|
||||
// Nothing to clean up.
|
||||
}
|
||||
|
||||
|
|
@ -39,36 +35,25 @@ export class MarpPreviewView extends ItemView {
|
|||
displaySlides(basePath: string, markdownText: string) {
|
||||
console.log("Marp Preview Display Slides");
|
||||
|
||||
const baseHref = basePath;
|
||||
const container = this.containerEl.children[1];
|
||||
container.empty();
|
||||
|
||||
var { html, css } = this.marp.render(markdownText);
|
||||
console.log(html);
|
||||
html = html.replace(/background-image:url\("/g, `background-image:url("${baseHref}/`);
|
||||
//console.log(html);
|
||||
|
||||
// Replace Backgorund Url for images
|
||||
html = html.replace(/background-image:url\("/g, `background-image:url("${basePath}/`);
|
||||
|
||||
const htmlFile = `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<base href="${baseHref}"></base>
|
||||
<base href="${basePath}"></base>
|
||||
<style>${css}</style>
|
||||
</head>
|
||||
<body>${html}</body>
|
||||
</html>
|
||||
`
|
||||
container.innerHTML = htmlFile;
|
||||
//this.contentEl.createDiv({ text: "hello word!" })
|
||||
|
||||
// const viewContent = this.containerEl.children[1];
|
||||
|
||||
// viewContent.empty();
|
||||
// viewContent.addClass('reveal-preview-view');
|
||||
// viewContent.createEl('iframe', {
|
||||
// attr: {
|
||||
// // @ts-ignore:
|
||||
// src: this.url,
|
||||
// sandbox: 'allow-scripts allow-same-origin allow-popups',
|
||||
// },
|
||||
// });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue