@ohos-ports/remark-unlink-protocols 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/index.d.ts +1 -0
- package/index.js +6 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +74 -0
- package/package.json +44 -0
- package/readme.md +164 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 nextcloud-libraries
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./lib/index.js";
|
package/index.js
ADDED
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Only keep links with the given protocols.
|
|
3
|
+
*
|
|
4
|
+
* @param {object} options Options
|
|
5
|
+
* @param {string[]} options.except - Protocols to exclude. Defauls to `['http', 'https']`.
|
|
6
|
+
* @returns
|
|
7
|
+
* Transform.
|
|
8
|
+
*/
|
|
9
|
+
export default function remarkUnlinkProtocols(options?: {
|
|
10
|
+
except: string[];
|
|
11
|
+
}): (tree: Root) => undefined;
|
|
12
|
+
export type PhrasingContent = import("mdast").PhrasingContent;
|
|
13
|
+
export type Root = import("mdast").Root;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('mdast').PhrasingContent} PhrasingContent
|
|
8
|
+
* @typedef {import('mdast').Root} Root
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {squeezeParagraphs} from 'mdast-squeeze-paragraphs'
|
|
12
|
+
import {visit} from 'unist-util-visit'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Only keep links with the given protocols.
|
|
16
|
+
*
|
|
17
|
+
* @param {object} options Options
|
|
18
|
+
* @param {string[]} options.except - Protocols to exclude. Defauls to `['http', 'https']`.
|
|
19
|
+
* @returns
|
|
20
|
+
* Transform.
|
|
21
|
+
*/
|
|
22
|
+
export default function remarkUnlinkProtocols(
|
|
23
|
+
options = {except: ['http', 'https']}
|
|
24
|
+
) {
|
|
25
|
+
/**
|
|
26
|
+
* Transform.
|
|
27
|
+
*
|
|
28
|
+
* @param {Root} tree
|
|
29
|
+
* Tree.
|
|
30
|
+
* @returns {undefined}
|
|
31
|
+
* Nothing.
|
|
32
|
+
*/
|
|
33
|
+
return function (tree) {
|
|
34
|
+
/** @type {Map<string, string>} */
|
|
35
|
+
const definitions = new Map()
|
|
36
|
+
|
|
37
|
+
// Find definitions to look up linkReferences.
|
|
38
|
+
visit(tree, 'definition', function (node, index, parent) {
|
|
39
|
+
definitions.set(node.identifier, node.url)
|
|
40
|
+
if (parent && typeof index === 'number') {
|
|
41
|
+
const url = node.url
|
|
42
|
+
if (
|
|
43
|
+
url &&
|
|
44
|
+
url.includes(':') &&
|
|
45
|
+
!options.except.some((proto) => url.startsWith(`${proto}:`))
|
|
46
|
+
) {
|
|
47
|
+
parent.children.splice(index, 1)
|
|
48
|
+
return index
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
visit(tree, function (node, index, parent) {
|
|
54
|
+
if (
|
|
55
|
+
parent &&
|
|
56
|
+
typeof index === 'number' &&
|
|
57
|
+
(node.type === 'link' || node.type === 'linkReference')
|
|
58
|
+
) {
|
|
59
|
+
const url =
|
|
60
|
+
node.type === 'link' ? node.url : definitions.get(node.identifier)
|
|
61
|
+
if (
|
|
62
|
+
url &&
|
|
63
|
+
url.includes(':') &&
|
|
64
|
+
!options.except.some((proto) => url.startsWith(`${proto}:`))
|
|
65
|
+
) {
|
|
66
|
+
parent.children.splice(index, 1, ...node.children)
|
|
67
|
+
return index
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
squeezeParagraphs(tree)
|
|
73
|
+
}
|
|
74
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ohos-ports/remark-unlink-protocols",
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
|
+
"description": "remark plugin to remove links and references based on their protocol",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"definitions",
|
|
8
|
+
"filter",
|
|
9
|
+
"links",
|
|
10
|
+
"markdown",
|
|
11
|
+
"mdast",
|
|
12
|
+
"plaintext",
|
|
13
|
+
"plugin",
|
|
14
|
+
"protocol",
|
|
15
|
+
"references",
|
|
16
|
+
"remark",
|
|
17
|
+
"remark-plugin",
|
|
18
|
+
"remove",
|
|
19
|
+
"text",
|
|
20
|
+
"unified"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
25
|
+
"directory": "ports/remark-unlink-protocols/1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"bugs": "https://github.com/ohos-ports/ohos-ports/issues",
|
|
28
|
+
"author": "Max <max@nextcloud.com>",
|
|
29
|
+
"contributors": [],
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"type": "module",
|
|
32
|
+
"exports": "./index.js",
|
|
33
|
+
"files": [
|
|
34
|
+
"lib/",
|
|
35
|
+
"index.d.ts",
|
|
36
|
+
"index.js"
|
|
37
|
+
],
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@types/mdast": "^4.0.0",
|
|
40
|
+
"mdast-squeeze-paragraphs": "^6.0.0",
|
|
41
|
+
"unist-util-visit": "^5.0.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {}
|
|
44
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
|
3
|
+
- SPDX-License-Identifier: MIT
|
|
4
|
+
-->
|
|
5
|
+
|
|
6
|
+
# remark-unlink-protocols
|
|
7
|
+
|
|
8
|
+
[](https://api.reuse.software/info/github.com/nextcloud-libraries/remark-unlink-protocol)
|
|
9
|
+
|
|
10
|
+
**[remark][]** plugin to turn links into plain text based on their protocols.
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
|
|
14
|
+
* [What is this?](#what-is-this)
|
|
15
|
+
* [When should I use this?](#when-should-i-use-this)
|
|
16
|
+
* [Install](#install)
|
|
17
|
+
* [Use](#use)
|
|
18
|
+
* [API](#api)
|
|
19
|
+
* [Types](#types)
|
|
20
|
+
* [Compatibility](#compatibility)
|
|
21
|
+
* [Security](#security)
|
|
22
|
+
* [Contribute](#contribute)
|
|
23
|
+
* [License](#license)
|
|
24
|
+
|
|
25
|
+
## What is this?
|
|
26
|
+
|
|
27
|
+
This package is a [unified][] ([remark][]) plugin to remove all links
|
|
28
|
+
that use other protocols than the ones specified
|
|
29
|
+
(`http` and `https` by default).
|
|
30
|
+
|
|
31
|
+
## When should I use this?
|
|
32
|
+
|
|
33
|
+
This project is useful if you want to limit the protocols
|
|
34
|
+
that can be linked to when processing markdown.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
This package is [ESM only][esm].
|
|
39
|
+
In Node.js (version 16+), install with [npm][]:
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
npm install remark-unlink-protocols
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Use
|
|
46
|
+
|
|
47
|
+
Say we have the following file `example.md` and apply `remark-unlink-protocols`
|
|
48
|
+
with the default protocols (`http` and `https`).
|
|
49
|
+
|
|
50
|
+
```markdown
|
|
51
|
+
# Uranus
|
|
52
|
+
|
|
53
|
+
**Uranus** is the seventh [planet](/wiki/Planet 'Planet') from the Sun and is a
|
|
54
|
+
gaseous cyan [ice giant](/wiki/Ice_giant 'Ice giant').
|
|
55
|
+
|
|
56
|
+
Photograph of Uranus in true colour by Voyager 2 in 1986:
|
|
57
|
+
|
|
58
|
+
_-_JPEG_converted.jpg>)
|
|
60
|
+
|
|
61
|
+
Send comments to [me](mailto:test@domain.example)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
…and a module `example.js`:
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import {remark} from 'remark'
|
|
68
|
+
import remarkUnlinkProtocols from 'remark-unlink-protocols'
|
|
69
|
+
import {read} from 'to-vfile'
|
|
70
|
+
|
|
71
|
+
const file = await remark()
|
|
72
|
+
.use(remarkUnlinkProtocols, { except: 'mailto' })
|
|
73
|
+
.process(await read('example.md'))
|
|
74
|
+
|
|
75
|
+
console.log(String(file))
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
…then running `node example.js` yields:
|
|
79
|
+
|
|
80
|
+
```markdown
|
|
81
|
+
# Uranus
|
|
82
|
+
|
|
83
|
+
**Uranus** is the seventh planet from the Sun and is a
|
|
84
|
+
gaseous cyan ice giant.
|
|
85
|
+
|
|
86
|
+
Photograph of Uranus in true colour by Voyager 2 in 1986:
|
|
87
|
+
|
|
88
|
+
_-_JPEG_converted.jpg>)
|
|
90
|
+
|
|
91
|
+
The Voyager project is managed for NASA by the Jet Propulsion
|
|
92
|
+
Laboratory.
|
|
93
|
+
|
|
94
|
+
Send comments to [me](mailto:test@domain.example).
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API
|
|
98
|
+
|
|
99
|
+
This package exports no identifiers.
|
|
100
|
+
The default export is `remarkUnlinkProtocols`.
|
|
101
|
+
|
|
102
|
+
#### `unified().use(remarkUnlinkProtocols)`
|
|
103
|
+
|
|
104
|
+
Remove all links and references that do not use the `http` or `https` protocols.
|
|
105
|
+
|
|
106
|
+
###### Parameters
|
|
107
|
+
|
|
108
|
+
`except`: `string[]` - protocols to keep - `['http', 'https']` by default.
|
|
109
|
+
|
|
110
|
+
###### Returns
|
|
111
|
+
|
|
112
|
+
Transform ([`Transformer`][unified-transformer]).
|
|
113
|
+
|
|
114
|
+
## Types
|
|
115
|
+
|
|
116
|
+
This package is fully typed with [TypeScript][].
|
|
117
|
+
It exports no additional types.
|
|
118
|
+
|
|
119
|
+
## Compatibility
|
|
120
|
+
|
|
121
|
+
The current release, `remark-unlink-protocols@^1` is compatible with Node.js 22.
|
|
122
|
+
|
|
123
|
+
This plugin works with `unified` version 3+ and `remark` version 4+.
|
|
124
|
+
|
|
125
|
+
## Security
|
|
126
|
+
|
|
127
|
+
Use of `remark-unlink-protocols` does not involve
|
|
128
|
+
**[rehype][]** (**[hast][]**) or user content
|
|
129
|
+
so there are no openings for [cross-site scripting (XSS)][wiki-xss]
|
|
130
|
+
attacks.
|
|
131
|
+
|
|
132
|
+
## Contribute
|
|
133
|
+
|
|
134
|
+
[Nextcloud][nc-coc] and [Remarkjs][coc] have a code of conduct.
|
|
135
|
+
By interacting with the respective repositories, organization, or community
|
|
136
|
+
you agree to abide by its terms.
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
[MIT][license] © 2025 Nextcloud GmbH and Nextcloud contributors
|
|
141
|
+
|
|
142
|
+
[coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md
|
|
143
|
+
|
|
144
|
+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
|
145
|
+
|
|
146
|
+
[hast]: https://github.com/syntax-tree/hast
|
|
147
|
+
|
|
148
|
+
[license]: LICENSE
|
|
149
|
+
|
|
150
|
+
[nc-coc]: https://nextcloud.com/contribute/code-of-conduct/
|
|
151
|
+
|
|
152
|
+
[npm]: https://docs.npmjs.com/cli/install
|
|
153
|
+
|
|
154
|
+
[rehype]: https://github.com/rehypejs/rehype
|
|
155
|
+
|
|
156
|
+
[remark]: https://github.com/remarkjs/remark
|
|
157
|
+
|
|
158
|
+
[typescript]: https://www.typescriptlang.org
|
|
159
|
+
|
|
160
|
+
[unified]: https://github.com/unifiedjs/unified
|
|
161
|
+
|
|
162
|
+
[unified-transformer]: https://github.com/unifiedjs/unified#transformer
|
|
163
|
+
|
|
164
|
+
[wiki-xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
|