Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to title text substitution #47

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Copy title, URL, selection as Markdown
- [Strong](#strong)
- [Link style](#link-style)
- [Link reference style](#link-reference-style)
- [Advanced](#advanced)
- [Contributing](#contributing)
- [License](#license)
- [Acknowledgements](#acknowledgements)
Expand Down Expand Up @@ -184,6 +185,12 @@ To change shortcut key, click gear icon on top-right and click **Manage Extensio
[reference with shortcut style]: https://example.com
```

### Advanced

- Title Substitution -- line separated texts which will be removed from title text. If you add ` - Mozilla | MDN` to the textbox, the copied text wil be:
- From `[Add-ons - Mozilla | MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons)`
- To `[Add-ons](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons)`

## Contributing

Please read [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"dependencies": {
"clipboard-copy": "^2.0.1",
"regex-escape": "^3.4.9",
"turndown": "4.0.2",
"turndown-plugin-gfm": "^1.0.2"
},
Expand Down
17 changes: 14 additions & 3 deletions src/copy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { doCopy, getSelectionAsMarkdown } from "./lib/util";
const RegexEscape = require("regex-escape");

async function main() {
try {
Expand All @@ -19,10 +20,20 @@ async function main() {
options.gfm = typeof options.gfm === "undefined" ? false : options.gfm;
options.linkWithoutStyling = typeof options.linkWithoutStyling === "undefined" ? false : options.linkWithoutStyling;
options.img = typeof options.img === "undefined" ? false : options.img;
options.titleSubstitution = typeof options.titleSubstitution === "undefined" ? "" : options.titleSubstitution;

let text = options.linkWithoutStyling
? `${document.title} (${document.URL})`
: `[${document.title}](${document.URL})`;
let title = document.title;
if (options.titleSubstitution !== "") {
let pattern = new RegExp(
options.titleSubstitution
.split(/\n/)
.map(e => `(${RegexEscape(e)})`)
.join("|"),
"g"
);
title = title.replace(pattern, "");
}
let text = options.linkWithoutStyling ? `${title} (${document.URL})` : `[${title}](${document.URL})`;
let selection = getSelectionAsMarkdown(options);

if (selection.output !== "") {
Expand Down
4 changes: 3 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ document.addEventListener("DOMContentLoaded", () => {
document.querySelector("#gfm").checked = typeof result.gfm === "undefined" ? false : result.gfm;
document.querySelector("#linkWithoutStyling").checked = typeof result.linkWithoutStyling === "undefined" ? false : result.linkWithoutStyling;
document.querySelector("#img").checked = typeof result.img === "undefined" ? false : result.img;
document.querySelector("#titleSubstitution").value = typeof result.titleSubstitution === "undefined" ? "" : result.titleSubstitution;
},
error => console.log(`Error: ${error}`)
);
Expand All @@ -47,6 +48,7 @@ document.querySelector("form").addEventListener("submit", e => {
mathjax: document.querySelector("#mathjax").checked,
gfm: document.querySelector("#gfm").checked,
linkWithoutStyling: document.querySelector("#linkWithoutStyling").checked,
img: document.querySelector("#img").checked
img: document.querySelector("#img").checked,
titleSubstitution: document.querySelector("#titleSubstitution").value
});
});
9 changes: 9 additions & 0 deletions src/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
button:active {
background-color: #006cb7;
}

textarea {
width: 100%;
}
</style>
</head>

Expand Down Expand Up @@ -182,6 +186,11 @@ <h2>Markdown</h2>

</table>
</div>
<div>
<h2>Advanced</h2>
<label for="titleSubstitution">Title Substitution -- line separated texts which will be removed from title text</label>
<textarea id="titleSubstitution" rows="5"></textarea>
</div>

<button type="submit">Save</button>
</form>
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7101,6 +7101,11 @@ regex-cache@^0.4.2:
dependencies:
is-equal-shallow "^0.1.3"

regex-escape@^3.4.9:
version "3.4.9"
resolved "https://registry.yarnpkg.com/regex-escape/-/regex-escape-3.4.9.tgz#407b413562e2373d56994ff213e693658ea4f8f7"
integrity sha512-Cv9rjwyQwVhn3L097ysanWsEElurmxDj6Cc4Ut23z7e6hzRbrNvF3Le7yAciMfuzyb0sZwSr0ZHunMNCIoy2/g==

regex-not@^1.0.0, regex-not@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
Expand Down