Skip to content

Commit

Permalink
feature: added option to copy HTML fragment only
Browse files Browse the repository at this point in the history
Don't add a HTML header with styling nor body tag.

Closes #27
  • Loading branch information
Martijn committed Sep 13, 2023
1 parent 8103afe commit f914773
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3,416 deletions.
38 changes: 34 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,17 @@ class CopyDocumentAsHTMLSettingsTab extends PluginSettingTab {
containerEl.createEl('h3', {text: 'Rendering'});

new Setting(containerEl)
.setName('Remove front-matter sections')
.setName('Copy HTML fragment only')
.setDesc("If checked, only generate a HTML fragment and not a full HTML document. This excludes the header, and effectively disables all styling.")
.addToggle(toggle => toggle
.setValue(this.plugin.settings.bareHtmlOnly)
.onChange(async (value) => {
this.plugin.settings.bareHtmlOnly = value;
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Remove properties / front-matter sections')
.setDesc("If checked, the YAML content between --- lines at the front of the document are removed. If you don't know what this means, leave it on.")
.addToggle(toggle => toggle
.setValue(this.plugin.settings.removeFrontMatter)
Expand All @@ -953,7 +963,7 @@ class CopyDocumentAsHTMLSettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName('Remove dataview metadata lines')
.setDesc(CopyDocumentAsHTMLSettingsTab.createFragmentWithHTML(`
<p>Remove lines that only contain dataview meta-data, eg. \"rating:: 9\". Metadata between square brackets is left intact.</p>
<p>Remove lines that only contain dataview meta-data, eg. "rating:: 9". Metadata between square brackets is left intact.</p>
<p>Current limitations are that lines starting with a space are not removed, and lines that look like metadata in code blocks are removed if they don't start with a space</p>`))
.addToggle(toggle => toggle
.setValue(this.plugin.settings.removeDataviewMetadataLines)
Expand Down Expand Up @@ -1054,6 +1064,11 @@ type CopyDocumentAsHTMLSettings = {

/** Style-sheet */
styleSheet: string;

/**
* Only generate the HTML body, don't include the <head> section
*/
bareHtmlOnly: boolean;
}

const DEFAULT_SETTINGS: CopyDocumentAsHTMLSettings = {
Expand All @@ -1065,7 +1080,8 @@ const DEFAULT_SETTINGS: CopyDocumentAsHTMLSettings = {
removeDataviewMetadataLines: false,
formatAsTables: false,
footnoteHandling: FootnoteHandling.REMOVE_LINK,
styleSheet: DEFAULT_STYLESHEET
styleSheet: DEFAULT_STYLESHEET,
bareHtmlOnly: false
}

export default class CopyDocumentAsHTMLPlugin extends Plugin {
Expand Down Expand Up @@ -1147,7 +1163,19 @@ export default class CopyDocumentAsHTMLPlugin extends Plugin {
}

private async copyFromView(activeView: MarkdownView, onlySelected: boolean) {
if (!activeView.editor) {
console.error('No editor in active view, nothing to copy');
return;
}

if (!activeView.file) {
// should not happen if we have an editor in the active view ?
console.error('No file in active view, nothing to copy');
return;
}

const markdown = onlySelected ? activeView.editor.getSelection() : activeView.data;

const path = activeView.file.path;
const name = activeView.file.name;
return this.doCopy(markdown, path, name);
Expand Down Expand Up @@ -1180,7 +1208,9 @@ export default class CopyDocumentAsHTMLPlugin extends Plugin {
ppIsProcessing = true;

const htmlBody = await copier.renderDocument(markdown, path);
const htmlDocument = htmlTemplate(this.settings.styleSheet, htmlBody.outerHTML, name);
const htmlDocument = this.settings.bareHtmlOnly
? htmlBody.outerHTML
: htmlTemplate(this.settings.styleSheet, htmlBody.outerHTML, name);

const data =
new ClipboardItem({
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "copy-document-as-html",
"name": "Copy document as HTML",
"version": "0.5.0",
"minAppVersion": "1.3.5",
"version": "0.6.0",
"minAppVersion": "1.4.11",
"description": "Copy the current document to clipboard as HTML, including images, diagrams etc...",
"author": "mvdkwast",
"authorUrl": "https://github.com/mvdkwast",
Expand Down
Loading

0 comments on commit f914773

Please sign in to comment.