File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ import remark from 'remark' ;
2
+ import noAutoLink from '../noAutoLinkRemarkPlugin' ;
3
+
4
+ const transform = markdown =>
5
+ remark ( )
6
+ . use ( noAutoLink )
7
+ . processSync ( markdown )
8
+ . toString ( ) ;
9
+
10
+ it ( 'should not convert URLs to auto links' , ( ) => {
11
+ const markdown = 'http://example.com' ;
12
+ const result = transform ( markdown ) ;
13
+ expect ( result . trim ( ) ) . toBe ( '[http://example.com](http://example.com "http://example.com")' ) ;
14
+ } ) ;
15
+
16
+ it ( 'should keep full inks as is' , ( ) => {
17
+ const markdown = '[Pizza](http://example.com)' ;
18
+ const result = transform ( markdown ) ;
19
+ expect ( result . trim ( ) ) . toBe ( markdown ) ;
20
+ } ) ;
Original file line number Diff line number Diff line change 1
1
const remark = require ( 'remark' ) ;
2
2
const visit = require ( 'unist-util-visit' ) ;
3
3
const highlightCode = require ( './highlightCode' ) ;
4
+ const noAutoLink = require ( './noAutoLinkRemarkPlugin' ) ;
4
5
5
6
function highlight ( ) {
6
7
return ast => {
@@ -19,6 +20,7 @@ function highlight() {
19
20
module . exports = function highlightCodeInMarkdown ( markdown ) {
20
21
return remark ( )
21
22
. use ( highlight )
23
+ . use ( noAutoLink )
22
24
. processSync ( markdown )
23
25
. toString ( ) ;
24
26
} ;
Original file line number Diff line number Diff line change
1
+ const visit = require ( 'unist-util-visit' ) ;
2
+
3
+ // Return a Remark AST link node's link text
4
+ const getLinkValue = node =>
5
+ node . children . reduce ( ( value , child ) => {
6
+ if ( child . type === 'text' ) {
7
+ value += child . value ;
8
+ }
9
+ return value ;
10
+ } , '' ) ;
11
+
12
+ /**
13
+ * Prevent printing URLs as auto links (<http://example.com>).
14
+ * Remark prints all links without a text as auto links, so we're adding a URL
15
+ * as a title. It has an unfortunate side effect: a link has a title of
16
+ * "http://..."
17
+ *
18
+ * @return {Object }
19
+ */
20
+ module . exports = function noAutoLinkRemarkPlugin ( ) {
21
+ return ast => {
22
+ visit ( ast , 'link' , node => {
23
+ const value = getLinkValue ( node ) ;
24
+
25
+ if ( value === node . url ) {
26
+ node . title = node . url ;
27
+ }
28
+ } ) ;
29
+ } ;
30
+ } ;
You can’t perform that action at this time.
0 commit comments