Skip to content

Fix #791 #866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cypress/inline/embed-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Embedded file order

Then the content of `delayed.md` `non-delayed.md` and will be displayed directly here in correct order

[delayed](_media/delayed.md ':include')

---

[non-delayed](_media/non-delayed.md ':include')

You can check the original content for [delayed.md](_media/delayed.md ':ignore'), [non-delayed.md](_media/non-delayed.md ':ignore').
3 changes: 2 additions & 1 deletion cypress/integration/sidebar/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ context('sidebar.configurations', () => {
const embedFilesIds = [
'embedded-file-type',
'embedded-code-fragments',
'embedded-file-order',
'tag-attribute',
'the-code-block-highlight',
];
Expand All @@ -336,7 +337,7 @@ context('sidebar.configurations', () => {
cy.get(`a.section-link[href='#/embed-files?id=${id}']`)
.click()
.then(() => {
cy.wait(500);
cy.wait(750);
cy.matchImageSnapshot();
});
});
Expand Down
15 changes: 12 additions & 3 deletions cypress/live.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ console.log('[e2e tests] : args passed to live server', args)
const params = {
port: args[0] || 3000,
root: args[1] || fixturePath,
open: false
open: false,
middleware: [
function(req, res, next) {
if (req.url === '/_media/delayed.md') {
setTimeout(next, 250);
} else {
next();
}
},
],
// NoBrowser: true
}
LiveServer.start(params)
};
LiveServer.start(params)
22 changes: 22 additions & 0 deletions cypress/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const setup = async () => {
// 1
const docsPath = path.join(process.cwd(), './docs')
const fixtureDocsPath = path.join(__dirname, './fixtures/docs')
const embeddedFiles = [
{
tag: '## Tag attribute',
srcFile: 'embed-order.md',
dstFile: 'embed-files.md',
},
];

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

// 1.3
embeddedFiles.forEach(({ tag, srcFile, dstFile }) => {
const content = fs.readFileSync(`${__dirname}/inline/${srcFile}`).toString();
const originalFile = `${fixtureDocsPath}/${dstFile}`;

let originalContent = fs
.readFileSync(originalFile)
.toString()
.split('\n');
const tagLine = originalContent.findIndex(l => l.indexOf(tag) >= 0);
originalContent.splice(tagLine, 0, content);

fs.writeFileSync(originalFile, originalContent.join('\n'));
});

// 2
console.log(
'[cypress test docs] Replacing content the tpl/index.html --> cypress/fixtures/docs/index.html'
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@

import { addMatchImageSnapshotCommand } from 'cypress-image-snapshot/command'
addMatchImageSnapshotCommand({
failureThreshold: 10.0,
failureThreshold: 0.10,
failureThresholdType: 'percent',
customDiffConfig: { threshold: 10.0 },
capture: 'viewport',
timeout: '60000'
})
Expand Down
7 changes: 7 additions & 0 deletions docs/_media/delayed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- This is from the `delayed.md`

```bash
sleep 1000
echo delayed
exit 0
```
7 changes: 7 additions & 0 deletions docs/_media/non-delayed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- This is from the `no-delay.md`

```bash
sleep 0
echo non-delayed
exit 0
```
39 changes: 29 additions & 10 deletions src/core/render/embed.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import stripIndent from 'strip-indent';
import { get } from '../fetch/ajax';
import { merge } from '../util/core';
import stripIndent from 'strip-indent';

const INCLUDE_COMPONENT = '__include__';
const cached = {};

function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
let token;
let step = 0;
let count = 1;
let count = 0;

if (!embedTokens.length) {
return cb({});
Expand Down Expand Up @@ -72,7 +73,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
}

cb({ token, embedToken });
if (++count >= step) {
if (++count >= embedTokens.length) {
cb({});
}
};
Expand All @@ -90,6 +91,23 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
}
}

function expandInclude(tokens) {
if (!tokens) {
return tokens;
}

const expandedTokens = [];
tokens.forEach(e => {
if (e.type === INCLUDE_COMPONENT && e.components) {
e.components.forEach(c => expandedTokens.push(c));
} else {
expandedTokens.push(e);
}
});

return expandedTokens;
}

export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
let hit = cached[raw];
if (hit) {
Expand Down Expand Up @@ -124,18 +142,19 @@ export function prerenderEmbed({ compiler, raw = '', fetch }, done) {
}
});

let moveIndex = 0;
walkFetchEmbed({ compile, embedTokens, fetch }, ({ embedToken, token }) => {
if (token) {
const index = token.index + moveIndex;

merge(links, embedToken.links);

tokens = tokens
.slice(0, index)
.concat(embedToken, tokens.slice(index + 1));
moveIndex += embedToken.length - 1;
tokens = tokens.slice(0, token.index).concat(
{
type: INCLUDE_COMPONENT,
components: embedToken,
},
tokens.slice(token.index + 1)
);
} else {
tokens = expandInclude(tokens);
cached[raw] = tokens.concat();
tokens.links = cached[raw].links = links;
done(tokens);
Expand Down
4 changes: 2 additions & 2 deletions src/core/render/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function getAndRemoveConfig(str = '') {

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