Skip to content
This repository was archived by the owner on Sep 8, 2023. It is now read-only.

Commit c0f81f4

Browse files
authored
Merge pull request #286 from juliushaertl/bugfix/noid/newlines
2 parents 6b23492 + 3782dc4 commit c0f81f4

5 files changed

Lines changed: 116 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
"vue": "^2.6.11"
3636
},
3737
"dependencies": {
38+
"clone": "^2.1.2",
3839
"core-js": "^3.6.4",
3940
"property-information": "^5.5.0",
4041
"rehype-add-classes": "^1.0.0",
4142
"rehype-react": "^6.1.0",
43+
"remark-breaks": "2.0.0",
4244
"remark-disable-tokenizers": "^1.0.0",
4345
"remark-external-links": "^8.0.0",
4446
"remark-parse": "^8.0.3",

src/RichText.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
<script>
2525
import unified from 'unified'
2626
import markdown from 'remark-parse'
27+
import breaks from 'remark-breaks'
2728
import remark2rehype from 'remark-rehype'
2829
import rehype2react from 'rehype-react'
29-
import remarkDisableBlocks from 'remark-disable-tokenizers'
30+
// import remarkDisableBlocks from 'remark-disable-tokenizers'
31+
import remarkDisableBlocks from './remarkDisableBlocks'
3032
import remarkExternalLinks from 'remark-external-links'
3133
import rehypeAddClasses from 'rehype-add-classes'
3234
@@ -147,13 +149,13 @@ export default {
147149
'strong',
148150
'emphasis',
149151
'deletion',
150-
'code',
151-
'break'
152+
'code'
153+
// 'break'
152154
// 'text' // do not uncomment or pluginComponent's register will have no point to insert itself
153155
]
154156
],
155157
block: [
156-
'blankLine',
158+
// 'blankLine',
157159
'indentedCode',
158160
'fencedCode',
159161
'blockquote',
@@ -176,6 +178,7 @@ export default {
176178
target: '_blank',
177179
rel: ['noopener noreferrer']
178180
})
181+
.use(breaks)
179182
.use(remarkDisableBlocks, this.remarkDisableOptions)
180183
.use(remark2rehype, {
181184
handlers: {

src/remarkDisableBlocks.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright (c) Zeste de Savoir (https://zestedesavoir.com)
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
/**
27+
* Patched version of https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-disable-tokenizers
28+
* to avoid newline issues described in https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-disable-tokenizers
29+
*/
30+
31+
import clone from 'clone'
32+
33+
const noop = () => false
34+
35+
const throwing = (msg) =>
36+
() => {
37+
throw new Error(msg)
38+
}
39+
40+
function ignore({ block = [], inline = [] } = {}) {
41+
if (block.length) {
42+
block
43+
.filter((key) => {
44+
if (Array.isArray(key)) return block.map(xs => xs[0]).includes(key[0])
45+
return block.includes(key)
46+
})
47+
.forEach((key) => {
48+
if (Array.isArray(key) && key.length === 2) {
49+
this.Parser.prototype.blockTokenizers[key[0]] = throwing(key[1])
50+
} else {
51+
this.Parser.prototype.blockTokenizers[key] = noop
52+
}
53+
})
54+
}
55+
56+
if (inline.length) {
57+
inline
58+
.filter((key) => {
59+
if (Array.isArray(key)) return inline.map(xs => xs[0]).includes(key[0])
60+
return inline.includes(key)
61+
})
62+
.forEach((key) => {
63+
let tokenizerName
64+
let replacer
65+
if (Array.isArray(key) && key.length === 2) {
66+
tokenizerName = key[0]
67+
replacer = throwing(key[1])
68+
} else {
69+
tokenizerName = key
70+
replacer = clone(noop)
71+
}
72+
if (this.Parser.prototype.inlineTokenizers[tokenizerName]) {
73+
Object
74+
.keys(this.Parser.prototype.inlineTokenizers[tokenizerName])
75+
.forEach((prop) => {
76+
replacer[prop] = this.Parser.prototype.inlineTokenizers[tokenizerName][prop]
77+
})
78+
}
79+
this.Parser.prototype.inlineTokenizers[tokenizerName] = replacer
80+
})
81+
}
82+
}
83+
84+
export default ignore

tests/richtext.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ describe('Foo', () => {
9494
expect(wrapper.find('a').attributes('href')).toEqual('https://example.com')
9595
})
9696

97+
it('properly inserts a newline', async() => {
98+
const wrapper = mount(RichText, {
99+
propsData: {
100+
text: 'Testwith a link to https://example.com \n go visit it',
101+
autolink: true
102+
}
103+
})
104+
105+
expect(wrapper.text()).toEqual('Testwith a link to https://example.com\ngo visit it')
106+
expect(wrapper.html()).toEqual(`<div class="rich-text--wrapper">
107+
<div>
108+
<p>Testwith a link to <a href="https://example.com" target="_blank" rel="noopener noreferrer" class="rich-text--external-link">https://example.com</a><br>
109+
go visit it</p>
110+
</div>
111+
</div>`)
112+
113+
expect(wrapper.find('a').attributes('href')).toEqual('https://example.com')
114+
})
115+
97116
it('properly inserts a link with brackets', async() => {
98117
const wrapper = mount(RichText, {
99118
propsData: {

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8946,6 +8946,10 @@ relateurl@0.2.x:
89468946
version "0.2.7"
89478947
resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9"
89488948

8949+
remark-breaks@2.0.0:
8950+
version "2.0.0"
8951+
resolved "https://registry.yarnpkg.com/remark-breaks/-/remark-breaks-2.0.0.tgz#96aa5078f5d582c328e7a9aff50b3167813aca58"
8952+
89498953
remark-disable-tokenizers@^1.0.0:
89508954
version "1.0.24"
89518955
resolved "https://registry.yarnpkg.com/remark-disable-tokenizers/-/remark-disable-tokenizers-1.0.24.tgz#98efd5ade2da46e8e6167cc3318c7cd93d15449d"

0 commit comments

Comments
 (0)