Skip to content

Commit d568ad5

Browse files
authored
Merge pull request #573 from NejcZdovc/feature/#350
Added split code blocks to home page
2 parents 92e1f78 + 8cb938b commit d568ad5

File tree

4 files changed

+125
-9
lines changed

4 files changed

+125
-9
lines changed

content/index.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ title: webpack
33
---
44
## Write your code.
55

6+
<div class="homepage__wrap">
7+
<div class="homepage__left">
8+
69
**app.js**
710

811
```js
@@ -11,6 +14,8 @@ import bar from './bar';
1114
bar();
1215
```
1316

17+
</div><div class="homepage__right">
18+
1419
**bar.js**
1520

1621
```js
@@ -19,8 +24,14 @@ export default function bar() {
1924
}
2025
```
2126

27+
</div>
28+
</div>
29+
2230
## Bundle with webpack.
2331

32+
<div class="homepage__wrap">
33+
<div class="homepage__left">
34+
2435
**webpack.config.js**
2536

2637
```js
@@ -32,6 +43,8 @@ module.exports = {
3243
}
3344
```
3445

46+
</div><div class="homepage__right">
47+
3548
**page.html**
3649

3750
```html
@@ -48,5 +61,8 @@ module.exports = {
4861

4962
Then run `webpack` on the command-line to create `bundle.js`.
5063

64+
</div>
65+
</div>
66+
5167
## It's that simple.
5268
## [Get Started](/get-started)

styles/homepage.scss

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.homepage {
2+
&__left,
3+
&__right {
4+
width: 100%;
5+
float: left;
6+
7+
@include break {
8+
width: 50%;
9+
padding: 0 15px;
10+
}
11+
}
12+
13+
&__wrap {
14+
display: block;
15+
16+
@include break {
17+
margin: 0 -30px;
18+
}
19+
20+
&:before,
21+
&:after {
22+
content: " ";
23+
display: table;
24+
}
25+
26+
&:after {
27+
clear: both;
28+
}
29+
}
30+
}

styles/index.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@import 'vars';
99
@import 'fonts';
1010
@import 'functions';
11+
@import 'mixins';
1112

1213
@import './reset';
1314

@@ -85,3 +86,4 @@ details:focus, summary:focus{
8586
}
8687

8788
@import './markdown';
89+
@import './homepage';

utilities/markdown.js

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = function(section) {
7171
rendered = rendered.replace(/\n.*?MARKDOWNSUMMARYSTART.*?\n/g, "<summary><span class='code-details-summary-span'>");
7272
rendered = rendered.replace(/\n.*?MARKDOWNSUMMARYEND.*?\n/g, "</span></summary>");
7373
}
74-
74+
7575
return rendered;
7676
};
7777

@@ -95,7 +95,8 @@ module.exports = function(section) {
9595
xhtml: false
9696
};
9797

98-
var tokens = parseQuotes(content);
98+
var tokens = parseContent(content);
99+
tokens.links = [];
99100

100101
return marked.parser(tokens, markedDefaults);
101102
},
@@ -112,23 +113,90 @@ module.exports = function(section) {
112113
};
113114
};
114115

115-
function parseQuotes(data) {
116-
var tokens = marked.lexer(data).map(function(t) {
116+
function parseContent(data) {
117+
var tokens = [];
118+
119+
marked.lexer(data).forEach(function(t) {
120+
// add custom quotes
117121
if (t.type === 'paragraph') {
118-
return parseCustomQuote(t, 'T>', 'tip') ||
122+
var quote = parseCustomQuote(t, 'T>', 'tip') ||
119123
parseCustomQuote(t, 'W>', 'warning') ||
120124
parseCustomQuote(t, '?>', 'todo') ||
121125
t;
122-
}
123126

124-
return t;
127+
tokens.push(quote);
128+
}
129+
// handle html
130+
else if (t.type === 'html') {
131+
tokens = tokens.concat(handleHTML(t));
132+
}
133+
// just add other types
134+
else {
135+
tokens.push(t);
136+
}
125137
});
126138

127-
tokens.links = [];
128-
129139
return tokens;
130140
}
131141

142+
function handleHTMLSplit(tokens, htmlArray, merging) {
143+
const htmlItem = htmlArray[0];
144+
htmlArray = htmlArray.slice(1);
145+
const tickSplit = htmlItem.split('`');
146+
const tickLength = tickSplit.length;
147+
148+
// detect start of the inline code
149+
if(merging.length === 0 && tickLength%2 === 0) {
150+
merging = htmlItem;
151+
}
152+
// append code inside the inline code
153+
else if(merging.length > 0 && tickLength === 1) {
154+
merging += htmlItem;
155+
}
156+
// finish inline code
157+
else if(merging.length > 0 && tickLength > 1) {
158+
htmlArray.unshift(tickSplit.slice(1, tickLength).join("`"));
159+
merging += tickSplit[0]+"`";
160+
tokens = tokens.concat(parseContent(merging));
161+
merging = "";
162+
} else if (merging.length === 0) {
163+
tokens = tokens.concat(parseContent(htmlItem));
164+
}
165+
166+
if(htmlArray.length === 0) {
167+
return tokens;
168+
}
169+
170+
return handleHTMLSplit(tokens, htmlArray, merging);
171+
}
172+
173+
function handleHTML(t) {
174+
let tokens = [];
175+
176+
// Split code in markdown, so that HTML inside code is not parsed
177+
const codeArray = t.text.split(/(```(.|\n)*```)/g).filter(v => (v && v !== '' && v !== '\n'));
178+
179+
// if only one item in codeArray, then it's already parsed
180+
if(codeArray.length == 1) {
181+
return t;
182+
}
183+
184+
codeArray.forEach(item => {
185+
// if item is not code, then check for html tags and parse accordingly
186+
if (item.indexOf('```') !== 0) {
187+
// split all html tags
188+
const htmlArray = item.split(/\s*(<[^>]*>)/g).filter(v => (v !== '' && v !== '\n'));
189+
tokens = handleHTMLSplit(tokens, htmlArray, "");
190+
}
191+
// normally parse code block
192+
else {
193+
tokens = tokens.concat(parseContent(item));
194+
}
195+
});
196+
197+
return tokens;
198+
}
199+
132200
function parseCustomQuote(token, match, className) {
133201
if (token.type === 'paragraph') {
134202
var text = token.text;

0 commit comments

Comments
 (0)