Skip to content

Commit

Permalink
Update CopyAsMarkdown to add format options
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotmoon committed May 13, 2024
1 parent 3177c55 commit 3fe642c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
62 changes: 62 additions & 0 deletions source/CopyAsMarkdown.popclipext/Config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// #popclip
// name: Copy as Markdown
// identifier: com.pilotmoon.popclip.extension.copy-as-markdown
// description: Copy web content as Markdown.
// icon: '>md.png'
// popclip version: 4225

const linkedom=require("linkedom");
const TurndownService=require("turndown");

exports.options = [
{
identifier: "headingStyle",
label: "Heading Style",
type: "multiple",
values: ["atx", "setext"],
valueLabels: ["ATX — # Heading", "Setext — Heading⏎======="],
},
{
identifier: "bulletListMarker",
label: "Bullet List Marker",
type: "multiple",
values: ["*", "-", "+"],
},
{
identifier: "emDelimiter",
label: "<em> Delimiter",
type: "multiple",
values: ["*", "_"],
},
{
identifier: "strongDelimiter",
label: "<strong> Delimiter",
type: "multiple",
values: ["**", "__"],
},
{
identifier: "linkStyle",
label: "Link Style",
type: "multiple",
values: ["inlined", "referenced"],
valueLabels: ["Inline — [text](url)", "Reference — [text][id]"],
}
];

function htmlToMarkdown(html, options) {
// generate DOM object from HTML
function JSDOM(html) {
return linkedom.parseHTML(html);
} // facade to work like jsdom
const { document } = new JSDOM(html);
// extract markdown using turndown
const turndownService = new TurndownService(options);
return turndownService.turndown(document);
}

exports.action = {
captureHtml: true,
code(input, options) {
popclip.copyText(htmlToMarkdown(input.html, options));
},
};
12 changes: 0 additions & 12 deletions source/CopyAsMarkdown.popclipext/Config.json

This file was deleted.

Binary file not shown.
18 changes: 14 additions & 4 deletions source/CopyAsMarkdown.popclipext/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@

Copy web page content as Markdown source code. Also works on plain text and RTF content.

<img src="https://github.com/pilotmoon/PopClip-Extensions/blob/master/source/CopyAsMarkdown/CopyAsMarkdown-demo.gif?raw=true" width="320px">
### Options

* **Heading Style**: Choose between ATX-style headings (`# Heading`) and Setext-style headings (`Heading\n=======`).
* **Bullet List Marker**: Choose between `*`, `-`, and `+` for bullet lists.
* **&lt;em&gt; Delimiter**: Choose between `*` and `_` for `<em>` elements.
* **&lt;strong&gt; Delimiter**: Choose between `**` and `__` for `<strong>` elements.
* **Link Style**: Choose between inlined links (`[text](url)`) and reference links (`[text][id]`).

## About

This is an extension for [PopClip](https://pilotmoon.com/popclip/).
This is an extension for [PopClip](https://www.popclip.app/).

### Author

Nick Moore

### Requirements

Requires PopClip 2021.11.
Requires PopClip 2023.7.

### Notes

The original extension used [html2text](https://pypi.python.org/pypi/html2text/3.200.3) by Aaron Swartz.

The updated JavaScript extension uses PopClip's inbuilt markdownifier, which uses [Turndown](https://github.com/mixmark-io/turndown).
The updated JavaScript extension uses [Turndown](https://github.com/mixmark-io/turndown).

Hat-tip to Brett Terpstra whose [Web Markdownifier](http://brettterpstra.com/2013/12/23/web-markdownifier-for-popclip/) extension inspired this one.

## Changelog

### 13 May 2024

* Added output format options as per [forum request](https://forum.popclip.app/t/updated-extension-copy-as-markdown/603/4).

### 3 Feb 2022

* Rewritten in JavaScript & Turndown.
Expand Down

0 comments on commit 3fe642c

Please sign in to comment.