Skip to content

Commit 5770bad

Browse files
committed
Fix out of order embedding of files
- added double quote as attribute marker - fixed inject order of embedded files - added e2e test covering order of embedded files
1 parent eb1a3ce commit 5770bad

15 files changed

+93
-18
lines changed

cypress/inline/embed-order.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Embedded file order
2+
3+
Then the content of `delayed.md` `non-delayed.md` and will be displayed directly here in correct order
4+
5+
[delayed](_media/delayed.md ':include')
6+
7+
---
8+
9+
[non-delayed](_media/non-delayed.md ':include')
10+
11+
You can check the original content for [delayed.md](_media/delayed.md ':ignore'), [non-delayed.md](_media/non-delayed.md ':ignore').

cypress/integration/sidebar/config.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ context('sidebar.configurations', () => {
326326
const embedFilesIds = [
327327
'embedded-file-type',
328328
'embedded-code-fragments',
329+
'embedded-file-order',
329330
'tag-attribute',
330331
'the-code-block-highlight',
331332
];
@@ -336,7 +337,7 @@ context('sidebar.configurations', () => {
336337
cy.get(`a.section-link[href='#/embed-files?id=${id}']`)
337338
.click()
338339
.then(() => {
339-
cy.wait(500);
340+
cy.wait(750);
340341
cy.matchImageSnapshot();
341342
});
342343
});

cypress/live.server.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ console.log('[e2e tests] : args passed to live server', args)
77
const params = {
88
port: args[0] || 3000,
99
root: args[1] || fixturePath,
10-
open: false
10+
open: false,
11+
middleware: [
12+
function(req, res, next) {
13+
if (req.url === '/_media/delayed.md') {
14+
setTimeout(next, 250);
15+
} else {
16+
next();
17+
}
18+
},
19+
],
1120
// NoBrowser: true
12-
}
13-
LiveServer.start(params)
21+
};
22+
LiveServer.start(params)

cypress/setup.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const setup = async () => {
2323
// 1
2424
const docsPath = path.join(process.cwd(), './docs')
2525
const fixtureDocsPath = path.join(__dirname, './fixtures/docs')
26+
const embeddedFiles = [
27+
{
28+
tag: '## Tag attribute',
29+
srcFile: 'embed-order.md',
30+
dstFile: 'embed-files.md',
31+
},
32+
];
2633

2734
// 1.1
2835
console.log('[cypress test docs] Copying the docs --> cypress/fixtures/docs')
@@ -38,6 +45,21 @@ const setup = async () => {
3845
copyDir.sync(fromPath, toPath)
3946
})
4047

48+
// 1.3
49+
embeddedFiles.forEach(({ tag, srcFile, dstFile }) => {
50+
const content = fs.readFileSync(`${__dirname}/inline/${srcFile}`).toString();
51+
const originalFile = `${fixtureDocsPath}/${dstFile}`;
52+
53+
let originalContent = fs
54+
.readFileSync(originalFile)
55+
.toString()
56+
.split('\n');
57+
const tagLine = originalContent.findIndex(l => l.indexOf(tag) >= 0);
58+
originalContent.splice(tagLine, 0, content);
59+
60+
fs.writeFileSync(originalFile, originalContent.join('\n'));
61+
});
62+
4163
// 2
4264
console.log(
4365
'[cypress test docs] Replacing content the tpl/index.html --> cypress/fixtures/docs/index.html'

cypress/support/commands.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626

2727
import { addMatchImageSnapshotCommand } from 'cypress-image-snapshot/command'
2828
addMatchImageSnapshotCommand({
29-
failureThreshold: 10.0,
29+
failureThreshold: 0.10,
3030
failureThresholdType: 'percent',
31-
customDiffConfig: { threshold: 10.0 },
3231
capture: 'viewport',
3332
timeout: '60000'
3433
})

docs/_media/delayed.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- This is from the `delayed.md`
2+
3+
```bash
4+
sleep 1000
5+
echo delayed
6+
exit 0
7+
```

docs/_media/non-delayed.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- This is from the `no-delay.md`
2+
3+
```bash
4+
sleep 0
5+
echo non-delayed
6+
exit 0
7+
```

src/core/render/embed.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import stripIndent from 'strip-indent';
12
import { get } from '../fetch/ajax';
23
import { merge } from '../util/core';
3-
import stripIndent from 'strip-indent';
44

5+
const INCLUDE_COMPONENT = '__include__';
56
const cached = {};
67

78
function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
89
let token;
910
let step = 0;
10-
let count = 1;
11+
let count = 0;
1112

1213
if (!embedTokens.length) {
1314
return cb({});
@@ -72,7 +73,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
7273
}
7374

7475
cb({ token, embedToken });
75-
if (++count >= step) {
76+
if (++count >= embedTokens.length) {
7677
cb({});
7778
}
7879
};
@@ -90,6 +91,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
9091
}
9192
}
9293

94+
function expandInclude(tokens) {
95+
if (!tokens) {
96+
return tokens;
97+
}
98+
99+
const expandedTokens = [];
100+
tokens.forEach(e => {
101+
if (e.type === INCLUDE_COMPONENT && e.components) {
102+
e.components.forEach(c => expandedTokens.push(c));
103+
} else {
104+
expandedTokens.push(e);
105+
}
106+
});
107+
108+
return expandedTokens;
109+
}
110+
93111
export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
94112
let hit = cached[raw];
95113
if (hit) {
@@ -124,18 +142,19 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
124142
}
125143
});
126144

127-
let moveIndex = 0;
128145
walkFetchEmbed({ compile, embedTokens, fetch }, ({ embedToken, token }) => {
129146
if (token) {
130-
const index = token.index + moveIndex;
131-
132147
merge(links, embedToken.links);
133148

134-
tokens = tokens
135-
.slice(0, index)
136-
.concat(embedToken, tokens.slice(index + 1));
137-
moveIndex += embedToken.length - 1;
149+
tokens = tokens.slice(0, token.index).concat(
150+
{
151+
type: INCLUDE_COMPONENT,
152+
components: embedToken,
153+
},
154+
tokens.slice(token.index + 1)
155+
);
138156
} else {
157+
tokens = expandInclude(tokens);
139158
cached[raw] = tokens.concat();
140159
tokens.links = cached[raw].links = links;
141160
done(tokens);

src/core/render/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export function getAndRemoveConfig(str = '') {
2323

2424
if (str) {
2525
str = str
26-
.replace(/^'/, '')
27-
.replace(/'$/, '')
26+
.replace(/^("|')/, '')
27+
.replace(/("|')$/, '')
2828
.replace(/(?:^|\s):([\w-]+:?)=?([\w-%]+)?/g, (m, key, value) => {
2929
if (key.indexOf(':') === -1) {
3030
config[key] = (value && value.replace(/"/g, '')) || true;

0 commit comments

Comments
 (0)