Skip to content

Commit 12bb518

Browse files
committed
feat: support enable/disable hyper link by forceHyperLink option
1 parent 3115eba commit 12bb518

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ var defaultOptions = {
127127
tab: 3 // examples: 4, 2, \t, \t\t
128128

129129
image: function (href, title, text) {} // function for overriding the default image handling.
130+
131+
// Boolean, force enable or disable hyper link, default is `undefined`
132+
// If undefined, it will dynamically determine whether the current environment supports hyperlink
133+
forceHyperLink: undefined
130134
};
131135
```
132136

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ var defaultOptions = {
5151
showSectionPrefix: true,
5252
reflowText: false,
5353
tab: 4,
54-
tableOptions: {}
54+
tableOptions: {},
55+
forceHyperLink: undefined
5556
};
5657

5758
function Renderer(options, highlightOptions) {
@@ -323,7 +324,12 @@ Renderer.prototype.link = function (href, title, text) {
323324

324325
var out = '';
325326

326-
if (supportsHyperlinks.stdout) {
327+
const { forceHyperLink } = this.o;
328+
329+
if (
330+
forceHyperLink === true ||
331+
(forceHyperLink !== false && supportsHyperlinks.stdout)
332+
) {
327333
let link = '';
328334
if (text) {
329335
link = this.o.href(this.emoji(text));

tests/options.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { notEqual, equal } from 'assert';
22
import Renderer from '../index.js';
33
import marked, { resetMarked } from './_marked.js';
4+
import ansiEscapes from 'ansi-escapes';
45

56
var identity = function (o) {
67
return o;
@@ -82,7 +83,7 @@ describe('Options', function () {
8283
equal(marked(listText, { renderer: r }), '\t* List Item\n\n');
8384
});
8485

85-
it('should support mulitple tab characters', function () {
86+
it('should support multiple tab characters', function () {
8687
var options = Object.assign({}, defaultOptions, { tab: '\t\t' });
8788
var r = new Renderer(options);
8889

@@ -110,6 +111,48 @@ describe('Options', function () {
110111
111112
IMAGE
112113
114+
`
115+
);
116+
});
117+
118+
it('should support disable hyper link by set forceHyperLink to false', function () {
119+
var options = Object.assign({}, defaultOptions, {
120+
forceHyperLink: false
121+
});
122+
var r = new Renderer(options);
123+
124+
var text = `
125+
# Title
126+
127+
[Github](https://www.github.com)
128+
`;
129+
equal(
130+
marked(text, { renderer: r }),
131+
`# Title
132+
133+
Github (https://www.github.com)
134+
135+
`
136+
);
137+
});
138+
139+
it('should support enable hyper link by set forceHyperLink to true', function () {
140+
var options = Object.assign({}, defaultOptions, {
141+
forceHyperLink: true
142+
});
143+
var r = new Renderer(options);
144+
145+
var text = `
146+
# Title
147+
148+
[Github](https://www.github.com)
149+
`;
150+
equal(
151+
marked(text, { renderer: r }),
152+
`# Title
153+
154+
${ansiEscapes.link('Github', 'https://www.github.com')}${'\n '}
155+
113156
`
114157
);
115158
});

0 commit comments

Comments
 (0)