Skip to content

Commit

Permalink
fix: Fix "Paste HTML as Markdown" command
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Dec 18, 2021
1 parent c189ae1 commit 8c324f8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"onCommand:memo.openDailyNote",
"onCommand:memo.openReferenceInDefaultApp",
"onCommand:memo.openReferenceBeside",
"onCommand:memo.pasteHtmlAsMarkdown",
"onCommand:memo.extractRangeToNewNote",
"onCommand:_memo.openReference",
"onCommand:_memo.cacheWorkspace",
Expand Down Expand Up @@ -72,6 +73,11 @@
"title": "Open link to the side",
"category": "Memo"
},
{
"command": "memo.pasteHtmlAsMarkdown",
"title": "Paste HTML as Markdown",
"category": "Memo"
},
{
"command": "memo.extractRangeToNewNote",
"title": "Extract range to a new note",
Expand Down Expand Up @@ -286,6 +292,7 @@
"ovsx": "^0.2.1",
"prettier": "^2.5.1",
"standard-version": "^9.3.2",
"terser-webpack-plugin": "^5.3.0",
"ts-jest": "^27.1.2",
"ts-loader": "^9.2.6",
"typescript": "^4.5.4",
Expand Down
36 changes: 20 additions & 16 deletions src/commands/pasteHtmlAsMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ const tdSettings = {
};

const pasteHtmlAsMarkdown = async () => {
const tdService = new TurndownService(tdSettings);
try {
const tdService = new TurndownService(tdSettings);

const clipboard = await readClipboard();
const clipboard = await readClipboard();

const markdown = tdService.turndown(clipboard);
const markdown = tdService.turndown(clipboard);

const editor = vscode.window.activeTextEditor;
const editor = vscode.window.activeTextEditor;

if (!editor) {
return;
}
if (!editor) {
return;
}

editor.edit((edit) => {
const current = editor.selection;
editor.edit((edit) => {
const current = editor.selection;

editor.selections.forEach((selection) => {
if (selection.isEmpty) {
edit.insert(selection.start, markdown);
} else {
edit.replace(current, markdown);
}
editor.selections.forEach((selection) => {
if (selection.isEmpty) {
edit.insert(selection.start, markdown);
} else {
edit.replace(current, markdown);
}
});
});
});
} catch (error) {
console.error(error);
}
};

export default pasteHtmlAsMarkdown;
28 changes: 23 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');

const config = {
module.exports = (env, argv) => ({
target: 'node',
entry: './src/extension.ts',
output: {
Expand Down Expand Up @@ -30,7 +31,24 @@ const config = {
},
],
},
plugins: [new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/)],
};

module.exports = config;
optimization:
argv.mode === 'production'
? {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
keep_fnames: /^(HTML|SVG)/, // https:/fgnass/domino/issues/144,
compress: {
passes: 2,
},
},
}),
],
}
: undefined,
plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/),
new webpack.IgnorePlugin({ resourceRegExp: /canvas|bufferutil|utf-8-validate/ }),
],
});
26 changes: 23 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4534,7 +4534,7 @@ jest-worker@^27.0.2:
merge-stream "^2.0.0"
supports-color "^8.0.0"

jest-worker@^27.4.5:
jest-worker@^27.4.1, jest-worker@^27.4.5:
version "27.4.5"
resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-27.4.5.tgz#d696e3e46ae0f24cff3fa7195ffba22889262242"
integrity sha512-f2s8kEdy15cv9r7q4KkzGXvlY0JTcmCbMHZBfSQDwW77REr45IDWwd0lksDFeVHH2jJ5pqb90T77XscrjeGzzg==
Expand Down Expand Up @@ -6125,7 +6125,7 @@ schema-utils@^3.0.0:
ajv "^6.12.5"
ajv-keywords "^3.5.2"

schema-utils@^3.1.0:
schema-utils@^3.1.0, schema-utils@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-3.1.1.tgz#bc74c4b6b6995c1d88f76a8b77bea7219e0c8281"
integrity sha512-Y5PQxS4ITlC+EahLuXaY86TXfR7Dc5lw294alXOq86JAHCihAIZfqv8nNCWvaEJvaC51uN9hbLGeV0cFBdH+Fw==
Expand Down Expand Up @@ -6260,7 +6260,7 @@ slice-ansi@^5.0.0:
ansi-styles "^6.0.0"
is-fullwidth-code-point "^4.0.0"

source-map-support@^0.5.17:
source-map-support@^0.5.17, source-map-support@~0.5.20:
version "0.5.21"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==
Expand Down Expand Up @@ -6628,6 +6628,17 @@ terser-webpack-plugin@^5.1.3:
source-map "^0.6.1"
terser "^5.7.0"

terser-webpack-plugin@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/terser-webpack-plugin/-/terser-webpack-plugin-5.3.0.tgz#21641326486ecf91d8054161c816e464435bae9f"
integrity sha512-LPIisi3Ol4chwAaPP8toUJ3L4qCM1G0wao7L3qNv57Drezxj6+VEyySpPw4B1HSO2Eg/hDY/MNF5XihCAoqnsQ==
dependencies:
jest-worker "^27.4.1"
schema-utils "^3.1.1"
serialize-javascript "^6.0.0"
source-map "^0.6.1"
terser "^5.7.2"

terser@^5.7.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/terser/-/terser-5.7.1.tgz#2dc7a61009b66bb638305cb2a824763b116bf784"
Expand All @@ -6637,6 +6648,15 @@ terser@^5.7.0:
source-map "~0.7.2"
source-map-support "~0.5.19"

terser@^5.7.2:
version "5.10.0"
resolved "https://registry.yarnpkg.com/terser/-/terser-5.10.0.tgz#b86390809c0389105eb0a0b62397563096ddafcc"
integrity sha512-AMmF99DMfEDiRJfxfY5jj5wNH/bYO09cniSqhfoyxc8sFoYIgkJy86G04UoZU5VjlpnplVu0K6Tx6E9b5+DlHA==
dependencies:
commander "^2.20.0"
source-map "~0.7.2"
source-map-support "~0.5.20"

test-exclude@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-6.0.0.tgz#04a8698661d805ea6fa293b6cb9e63ac044ef15e"
Expand Down

0 comments on commit 8c324f8

Please sign in to comment.