Skip to content

Commit

Permalink
feat: trailing slash option for connect middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianCataldo committed Mar 15, 2024
1 parent bc68173 commit 2feb573
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "og-images-generator",
"version": "0.0.9",
"version": "0.1.0",
"description": "Generate OG images from a static folder and / or a middleware.\nExtract metadata from HTML pages. No headless browser involved.\nComes as a CLI, API or plugins.",
"keywords": [
"og-images",
Expand Down
11 changes: 8 additions & 3 deletions src/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,14 @@ export const DEFAULT_OG_PATH_PREFIX = '/og/';
* @param {string} [pathPrefix]
* @returns {string}
*/
export function ogPathToPagePath(url, pathPrefix = DEFAULT_OG_PATH_PREFIX) {
export function ogPathToPagePath(
url,
pathPrefix = DEFAULT_OG_PATH_PREFIX,
trailingSlash = true,
) {
const suffix = trailingSlash ? '/' : '';
return url
.replace(pathPrefix ?? '', '')
.replace(/^index.png$/, '')
.replace(/\.png$/, '');
.replace(/^index.png$/, suffix)
.replace(/\.png$/, suffix);
}
7 changes: 6 additions & 1 deletion src/plugins/connect-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
* @typedef {() => Promise<import('../generate.js').UserConfig>} ConfigReloader
* @param {object} [options]
* @param {string} [options.pathPrefix] - Default: `/og/`
* @param {boolean} [options.trailingSlash] - Default: `true`
* @param {ConfigReloader} [options.configReloader]
* @returns {Promise<import('connect').NextHandleFunction>}
*/
Expand All @@ -26,7 +27,11 @@ export async function connectOgImagesGenerator(options) {
if (req.url.startsWith(prefix) === false) return next();

const base = 'http://' + req.rawHeaders[req.rawHeaders.indexOf('Host') + 1];
const path = ogPathToPagePath(req.url);
const path = ogPathToPagePath(
req.url,
options?.pathPrefix,
options?.trailingSlash,
);

const pageUrl = new URL(path, base).href;

Expand Down

0 comments on commit 2feb573

Please sign in to comment.