Skip to content
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
62 changes: 60 additions & 2 deletions src/CodeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class CodeManager {

reset() {
this.pages = {};
this.paginationPages = {};
this.paginationPages = {};
}

static normalizeBuckets(bucket) {
Expand All @@ -89,6 +91,16 @@ class CodeManager {
}
}

addPaginationUrls(url, subUrls = []) {
if(!subUrls.length) { return; }
if(!this.paginationPages[url]) {
this.paginationPages[url] = new Set();
}
for(let u of subUrls) {
this.paginationPages[url].add(u);
}
}

addToPage(pageUrl, code = [], bucket) {
if(!Array.isArray(code) && code) {
code = [code];
Expand Down Expand Up @@ -142,14 +154,56 @@ class CodeManager {

getBucketsForPage(pageData) {
let pageUrl = pageData.url;
if(this.paginationPages[pageUrl]) { // Merge
let result = new Set();
for(let url of this.paginationPages[pageUrl]) {
result = result.union(new Set(this.getBucketsForPage(url)));
}
return Array.from(result.entries());
}
if(!this.pages[pageUrl]) {
return [];
}
return Object.keys(this.pages[pageUrl]);
}

getRawForPage(pageData, buckets = undefined) {
let url = pageData.url;
_getRawForPagination(url, buckets) {
let result = new Set();
buckets = CodeManager.normalizeBuckets(buckets);
// Merge data from pagination sub-pages.
for(let subUrl of this.paginationPages[url]) {
if(!this.pages[subUrl]) { continue; }
for(let b of buckets) {
if(!this.pages[subUrl][b]) { continue; }
for(let entry of this.pages[subUrl][b]) {
result.add(entry);
}
}
}
return result;
}

_getRawForPagination(url, buckets) {
let result = new Set();
for(let subUrl of this.paginationPages[url]) {
if(!this.pages[subUrl]) { continue; }
for(let b of buckets) {
if(!this.pages[subUrl][b]) { continue; }
for(let entry of this.pages[subUrl][b]) {
result.add(entry);
}
}
}
return result;
}

_getRawForPage(url, buckets = undefined) {
// Merge data from pagination sub-pages.
if(this.paginationPages[url]) {
buckets = CodeManager.normalizeBuckets(buckets);
return this._getRawForPagination(url, buckets);
}

if(!this.pages[url]) {
debug("No bundle code found for %o on %o, %O", this.name, url, this.pages);
return new Set();
Expand All @@ -173,6 +227,10 @@ class CodeManager {

debug("Retrieving %o for %o (buckets: %o, entries: %o, size: %o)", this.name, url, buckets, set.size, size);
return set;
}

getRawForPage(pageData, buckets = undefined) {
return this._getRawForPage(pageData.url, buckets);
}

async getForPage(pageData, buckets = undefined) {
Expand Down
16 changes: 16 additions & 0 deletions src/eleventy.shortcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default function(eleventyConfig, pluginOptions = {}) {
}
});

let pagesUsingBundles = {};

// e.g. `getBundle` shortcode to get code in current page bundle
// bucket can be an array
// This shortcode name is not configurable on purpose (for wider plugin compatibility)
Expand All @@ -45,6 +47,20 @@ export default function(eleventyConfig, pluginOptions = {}) {
throw new Error(`Invalid bundle type: ${type}. Available options: ${Object.keys(managers)}`);
}

let url = explicitUrl || this.page?.url;
if(url) {
pagesUsingBundles[url] = true;
}
let paginationUrls = (this?.ctx?.pagination?.items || []).
filter((i) => !!(i?.url)).
map((i) => i.url);
if(paginationUrls.length) {
paginationUrls.forEach((u) => {
pagesUsingBundles[u] = true;
})
managers[type].addPaginationUrls(url, paginationUrls);
}

return OutOfOrderRender.getAssetKey("get", type, bucket);
});

Expand Down
5 changes: 5 additions & 0 deletions test/stubs/pagination-support/_includes/template.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% getBundle "css" %}

{%- for post in pagination.items -%}
{{ post.content | safe }}
{%- endfor -%}
15 changes: 15 additions & 0 deletions test/stubs/pagination-support/eleventy.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import bundlePlugin from "../../../eleventy.bundle.js";

export default function(eleventyConfig) {
eleventyConfig.addPlugin(bundlePlugin, {
force: true, // for testing
immediate: true,
// toFileDirectory: "bundle",
bundles: false,
});

// eleventyConfig.addBundle("css");
eleventyConfig.addBundle("css", {
bundleHtmlContentFromSelector: "style",
});
};
9 changes: 9 additions & 0 deletions test/stubs/pagination-support/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
pagination:
data: collections.all
size: 2
alias: posts
templateEngineOverride: njk,md
layout: "./template.njk"
---

12 changes: 12 additions & 0 deletions test/stubs/pagination-support/post1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: "Post 1"
templateEngineOverride: njk,md
---

{% css %}
<style>#foo { border: 1px solid blue; }</style>
{% endcss %}

# First Post!!!!!

*sigh*
12 changes: 12 additions & 0 deletions test/stubs/pagination-support/post2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: "Post 2"
templateEngineOverride: njk,md
---

{% css %}
<style>#bar { border: 1px solid red; }</style>
{% endcss %}

# Second

Was this a good idea?
12 changes: 12 additions & 0 deletions test/stubs/pagination-support/post3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: "Post 3"
templateEngineOverride: njk,md
---

{% css %}
<style>#baz { outline: 1px dotted orange; }</style>
{% endcss %}

# Third

**Running out of steam.**
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,22 @@ test("<style> plucked into bundle with buckets", async t => {
t.deepEqual(normalize(results[0].content), `<div></div><style>@layer layer1 { * { color: yellow }
body { color: blue } } @layer layer2 { * { color: red } } * { color: orange }</style>`)
});


test("Pagination support", async t => {
let elev = new Eleventy("test/stubs/pagination-support/", undefined, {
configPath: "test/stubs/pagination-support/eleventy.config.js",
config: function(eleventyConfig) {
eleventyConfig.setQuietMode(true);
}
});
await elev.write();
t.deepEqual(
normalize(fs.readFileSync("_site/index.html", "utf8")),
`<style>#foo { border: 1px solid blue; }</style>
<style>#bar { border: 1px solid red; }</style><h1>First Post!!!!!</h1>
<p><em>sigh</em></p>
<h1>Second</h1>
<p>Was this a good idea?</p>`
);
});