Skip to content

Commit

Permalink
fixed broken target-blank option and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
mnmandahalf committed Jan 27, 2023
1 parent af31478 commit 7e66ff4
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 13 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# add-link-rel-cli
CLI to add some attributes on html `<a>` tags.

[![npm version](https://badge.fury.io/js/add-link-rel-cli.svg)](https://badge.fury.io/js/add-link-rel-cli)

## Installation
```sh
npm i add-link-rel-cli
```

## Usage
```
Usage: add-link-rel [options] <filename>
Expand All @@ -10,6 +19,18 @@ Options:
-a, --autofix
--noreferrer
--noopener
--target-blank, treat only links with target="_blank"
--only-target-blank
-h, --help display help for command
```

## Example

sample.html
```html
<a target="_blank"><li>Sample Link</li></a>
```

```sh
$ add-link-rel --noreferrer --noopener test.html
<a target="_blank" rel="noreferrer noopenner"><li>Sample Link</li></a>
```
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const main = () => {
.option('-a, --autofix')
.option('--noreferrer')
.option('--noopener')
.option('--target-blank, treat only links with target="_blank"');
.option('--only-target-blank');

program.parse();
const options = program.opts();
const filename = program.args[0];

const relArray = [];
if(options.noreferrer) relArray.push('noreferrer');
if(options.noopener) relArray.push('noopenner');
if (options.noreferrer) relArray.push('noreferrer');
if (options.noopener) relArray.push('noopenner');
const relString = relArray.join(' ');

const fs = require('fs');
Expand All @@ -25,11 +25,11 @@ const main = () => {
const nodes = root.querySelectorAll('a');

nodes.forEach((node) => {
if(options.targetBlank && !isTargetBlank(node)) return;
if(relString.length != 0) node.setAttribute('rel', relString);
if (options.onlyTargetBlank && !isTargetBlank(node)) return;
if (relString.length != 0) node.setAttribute('rel', relString);
console.log(node.toString());
});
if(options.autofix) {
if (options.autofix) {
fs.writeFileSync(filename, root.toString());
}
}
Expand Down
146 changes: 144 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "add-link-rel-cli",
"version": "1.0.0",
"description": "",
"version": "1.1.0",
"description": "CLI to add some attributes on html <a> tags.",
"main": "src/index.js",
"keywords": [],
"author": "",
"keywords": ["CLI", "HTML"],
"author": "mnmandahalf",
"license": "MIT",
"dependencies": {
"commander": "^9.4.1",
Expand Down

0 comments on commit 7e66ff4

Please sign in to comment.