Skip to content

Commit

Permalink
better support for netlify identity + decap_cms
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 16, 2023
1 parent 547bc74 commit 7ffccb5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project try to adheres to [Semantic Versioning](https://semver.org/).
Go to the `v1` branch to see the changelog of Lume 1.

## [2.0.3] - Unreleased
### Changed
- `decap_cms` plugin: Add a script in the homepage to redirect to /admin/
when an invite token or recovery token is detected from netlify identity.

### Fixed
- `sitemap` plugin: Add the `xmlns` namespace for localized urls.
- `multilanguage` plugin:
Expand Down
15 changes: 15 additions & 0 deletions plugins/decap_cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ export default function (userOptions?: Options) {
</html>
`,
}));

// Redirect to the admin page from the home page if the URL has a token
if (options.identity === "netlify") {
const homePage = site.pages.find((page) => page.data.url === "/");
const document = homePage?.document;

if (document) {
const script = document.createElement("script");
script.innerHTML =
`if (document.location.hash.startsWith("#invite_token=") || document.location.hash.startsWith("#recovery_token=")) { document.location = "${
site.url(options.path)
}" + document.location.hash; }`;
document.head.appendChild(script);
}
}
});
};
}
Expand Down
40 changes: 38 additions & 2 deletions tests/__snapshots__/decap_cms.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ snapshot[`Decap CMS plugin 1`] = `
"/",
"/_data",
"/_data/decap_cms.yml",
"/index.md",
],
}
`;
Expand Down Expand Up @@ -156,7 +157,7 @@ local_backend: false
<title>Admin</title>
</head>
<body>
<link href="/admin/config.yml" type="text/yaml" rel="cms-config-url"><script src="https://unpkg.com/decap-cms@3.0.12/dist/decap-cms.js"></script>
<link href="/admin/config.yml" type="text/yaml" rel="cms-config-url"><script src="https://unpkg.com/decap-cms@3.0.12/dist/decap-cms.js"></script><script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
</body>
</html>
',
Expand All @@ -171,7 +172,7 @@ local_backend: false
<title>Admin</title>
</head>
<body>
<link href="/admin/config.yml" type="text/yaml" rel="cms-config-url"><script src="https://unpkg.com/decap-cms@3.0.12/dist/decap-cms.js"></script>
<link href="/admin/config.yml" type="text/yaml" rel="cms-config-url"><script src="https://unpkg.com/decap-cms@3.0.12/dist/decap-cms.js"></script><script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
</body>
</html>
',
Expand All @@ -188,5 +189,40 @@ local_backend: false
remote: undefined,
},
},
{
content: '<!DOCTYPE html>
<html><head><script>if (document.location.hash.startsWith("#invite_token=") || document.location.hash.startsWith("#recovery_token=")) { document.location = "/admin/" + document.location.hash; }</script></head><body><h1>Hello world</h1>
</body></html>',
data: {
basename: "index",
children: "<h1>Hello world</h1>
",
content: "# Hello world
",
date: [],
decap_cms: [
"backend",
"media_folder",
"public_folder",
],
mergedKeys: [
"tags",
],
page: [
"src",
"data",
],
paginate: "paginate",
search: [],
tags: "Array(0)",
url: "/",
},
src: {
asset: false,
ext: ".md",
path: "/index",
remote: undefined,
},
},
]
`;
1 change: 1 addition & 0 deletions tests/assets/decap_cms/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello world
1 change: 1 addition & 0 deletions tests/decap_cms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Deno.test("Decap CMS plugin", async (t) => {

site.use(decapCMS({
local: false,
identity: "netlify",
}));

await build(site);
Expand Down

0 comments on commit 7ffccb5

Please sign in to comment.