Skip to content

Commit ca9f99e

Browse files
Dobromir HristovKyle-Ye
andauthored
Code listing style fixes, remove extra line management (#667)
* Fix extra empty line issue on tutorial * refactor: change the CodeListing rendering Co-authored-by: Kyle <[email protected]>
1 parent 854c3cb commit ca9f99e

File tree

6 files changed

+27
-34
lines changed

6 files changed

+27
-34
lines changed

src/components/ContentNode/CodeListing.vue

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@
2323
</Filename>
2424
<div class="container-general">
2525
<!-- Do not add newlines in <pre>, as they'll appear in the rendered HTML. -->
26-
<pre><CodeBlock><span
26+
<pre><CodeBlock><template
2727
v-for="(line, index) in syntaxHighlightedLines"
28-
:class="['code-line-container',{ highlighted: isHighlighted(index) }]"
28+
><span
2929
:key="index"
30+
:class="['code-line-container',{ highlighted: isHighlighted(index) }]"
3031
><span
31-
v-show="showLineNumbers" class="code-number"
32+
v-if="showLineNumbers"
33+
class="code-number"
3234
:data-line-number="lineNumberFor(index)"
33-
/>
34-
<span
35-
class="code-line"
36-
v-html="line"
37-
/></span></CodeBlock></pre>
35+
/><span
36+
class="code-line"
37+
v-html="line"
38+
/></span><!-- This new line must stay -->
39+
</template></CodeBlock></pre>
3840
</div>
3941
</div>
4042
</template>
@@ -128,10 +130,13 @@ export default {
128130
@import 'docc-render/styles/_core.scss';
129131
130132
.code-line-container {
131-
display: flex;
133+
display: inline-block;
134+
width: 100%;
135+
box-sizing: border-box;
132136
}
133137
134138
.code-number {
139+
display: inline-block;
135140
padding: $code-number-padding;
136141
text-align: right;
137142
min-width: 2em;
@@ -167,18 +172,11 @@ pre {
167172
}
168173
169174
code {
170-
display: flex;
171-
flex-direction: column;
172175
white-space: pre;
173176
word-wrap: normal;
174177
flex-grow: 9999;
175178
}
176179
177-
.code-line-container {
178-
flex-shrink: 0;
179-
padding-right: 14px;
180-
}
181-
182180
.code-listing,
183181
.container-general {
184182
display: flex;

src/components/DocumentationTopic/ContentNode.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ $docs-code-listing-border-width: 1px !default;
4747
4848
pre {
4949
padding: var(--code-block-style-elements-padding);
50-
// setting it to 0 prevents browsers from adding extra right spacing, when having scrollbar
51-
padding-right: 0;
5250
5351
> code {
5452
@include font-styles(documentation-code-listing);

src/components/Tutorial/CodePreview.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export default {
178178
this.shouldDisplayHideLabel
179179
? this.$t('verbs.hide')
180180
: this.$t('verbs.show')
181-
}`;
181+
}`;
182182
},
183183
},
184184
methods: {
@@ -227,6 +227,11 @@ $duration: 0.2s;
227227
228228
/deep/ .code-listing {
229229
color: var(--text, var(--color-code-plain));
230+
231+
.code-line-container {
232+
// add extra padding, so we have extra space on right when scrolling is needed
233+
padding-right: 14px;
234+
}
230235
}
231236
232237
/deep/ pre {

src/utils/syntax-highlight.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This source file is part of the Swift.org open source project
33
*
4-
* Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
* Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
* Licensed under Apache License v2.0 with Runtime Library Exception
66
*
77
* See https://swift.org/LICENSE.txt for license information
@@ -192,7 +192,7 @@ function duplicateMultilineNode(element) {
192192

193193
// wrap each new line with the current element's class
194194
const result = getLines(element.innerHTML)
195-
.reduce((all, lineText) => `${all}<span class="${className}">${lineText || '\n\n'}</span>\n`, '');
195+
.reduce((all, lineText) => `${all}<span class="${className}">${lineText}</span>\n`, '');
196196

197197
// return a list of newly wrapped HTML elements
198198
return htmlToElements(result.trim());

tests/unit/components/ContentNode/CodeListing.spec.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ describe('CodeListing', () => {
5555
expect(listing.attributes('data-syntax')).toBe('swift');
5656

5757
const codeLineContainer = listing.find('span.code-line-container');
58-
const codeNumber = codeLineContainer.find('.code-number');
59-
// Code Number content should be empty to not get copied during user select
60-
expect(codeNumber.text()).toBe('');
58+
// Code Number content should not be rendered, if not needed
59+
expect(codeLineContainer.find('.code-number').exists()).toBe(false);
6160

6261
const codeLine = codeLineContainer.find('.code-line');
6362
expect(codeLine.text()).toBe('hello');
@@ -88,7 +87,6 @@ describe('CodeListing', () => {
8887
const shouldBeHighlighted = highlights.map(h => h.line).includes(index + 1);
8988

9089
const codeNumber = codeLineContainer.find('.code-number');
91-
// expect(codeNumber.text()).toBe(`${index + 1}`);
9290

9391
expect(codeNumber.attributes('data-line-number')).toBe(`${index + 1}`);
9492

tests/unit/utils/syntax-highlight.spec.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,11 @@ describe("syntax-highlight", () => {
107107
expect(sanitizedCode).toMatchInlineSnapshot(`
108108
<span class="syntax-keyword">let</span> multiline = <span class="syntax-string">""</span><span class="syntax-string">"</span>
109109
<span class="syntax-string">Needs</span>
110-
<span class="syntax-string">
111-
112-
</span>
110+
<span class="syntax-string"></span>
113111
<span class="syntax-string">Spaces</span>
114-
<span class="syntax-string">
115-
116-
</span>
112+
<span class="syntax-string"></span>
117113
<span class="syntax-string">Between</span>
118-
<span class="syntax-string">
119-
120-
</span>
114+
<span class="syntax-string"></span>
121115
<span class="syntax-string">Lines</span>
122116
<span class="syntax-string">"</span><span class="syntax-string">""</span>
123117
`);

0 commit comments

Comments
 (0)