Skip to content
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

Started working on new plugin remake #23

Merged
merged 9 commits into from
Aug 12, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ jobs:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
deno-version: v1.26.X
deno-version: v1.34.X

- run: deno task test
- run: deno task test
90 changes: 41 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Fresh SEO 🍋   [![Badge License]][License]

*Quickly creating sitemaps for your **Deno [Fresh project]**.*
Expand All @@ -7,26 +6,27 @@

## Getting Started

*Run the setup at the root of your project.*

```shell
deno run --allow-read --allow-write https://deno.land/x/fresh_seo/init.ts

```
*Import the plugin `freshSEOPlugin` in your Fresh app*

<br>
```ts
// ./main.ts
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

The following file should have been created:
import { freshSEOPlugin } from "https://deno.land/x/fresh_seo/mod.ts";

`routes/sitemap.xml.ts`
await start(manifest, {
plugins: [
freshSEOPlugin(manifest)
],
});

<br>
```

A basic sitemap should now be available at:

[`http://localhost:8000/sitemap.xml`][Localhost]

<br>
<br>

## How does it work?
Expand All @@ -42,50 +42,42 @@ A basic sitemap should now be available at:
*You will still have to map dynamic routes yourself!*

```ts
// ./routes/sitemap.xml.ts
import { SitemapContext } from 'https://deno.land/x/fresh_seo/mod.ts';
import { Handlers } from '$fresh/server.ts';
import manifest from '../fresh.gen.ts';

export const handler : Handlers = {
GET(request,context){
const sitemap = new SitemapContext(
'http://example.com', // put your domain here
manifest
);

// you can add additional page here
sitemap.add('/blog/hello-world');

return sitemap.render();
}
}
// ./main.ts
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

import { freshSEOPlugin } from "https://deno.land/x/fresh_seo/mod.ts";

await start(manifest, {
plugins: [
freshSEOPlugin(manifest, {
include: ["/blog/intro"]
})
],
});
```

*You can also remove unwanted routes*

```ts
// ./routes/sitemap.xml.ts
import { SitemapContext } from 'https://deno.land/x/fresh_seo/mod.ts';
import { Handlers } from '$fresh/server.ts';
import manifest from '../fresh.gen.ts';

export const handler : Handlers = {
GET(request,context){
const sitemap = new SitemapContext(
'http://example.com',
manifest
);

// You can remove unwanted routes here
sitemap.remove('/admin');

return sitemap.render();
}
}
// ./main.ts
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

import { freshSEOPlugin } from "https://deno.land/x/fresh_seo/mod.ts";

await start(manifest, {
plugins: [
freshSEOPlugin(manifest, {
exclude: [
"/blog/intro",
"/api/*"
]
})
],
});
```

<br>
<br>

## Testing
Expand Down
93 changes: 93 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions init.ts

This file was deleted.

9 changes: 5 additions & 4 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ export {
extname,
join,
resolve,
} from "https://deno.land/std@0.153.0/path/mod.ts";
} from "https://deno.land/std@0.197.0/path/mod.ts";
export {
assert,
assertStringIncludes,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
export { FakeTime } from "https://deno.land/[email protected]/testing/time.ts";
export { ensureFile } from "https://deno.land/[email protected]/fs/mod.ts";
} from "https://deno.land/[email protected]/testing/asserts.ts";
export { FakeTime } from "https://deno.land/[email protected]/testing/time.ts";
export { ensureFile } from "https://deno.land/[email protected]/fs/mod.ts";
export { filterFiles } from "https://deno.land/x/[email protected]/mod.ts";
89 changes: 0 additions & 89 deletions src/initFile.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { SitemapContext } from "./sitemap.ts";
export { freshSEOPlugin } from "./plugin.ts";
41 changes: 41 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { SitemapContext } from "./sitemap.ts";
import { Manifest, Plugin, RouteProps } from "./types.ts";

interface PluginOptions {
include?: Array<string | { path: string, options: RouteProps }>
exclude?: Array<string>
}

export const freshSEOPlugin = (manifest: Manifest, opts: PluginOptions = {}): Plugin => {
return {
name: "fresh-seo",
routes: [
{
path: "/sitemap.xml",
handler: (req) => {
const sitemap = new SitemapContext(req.url, manifest);

if (opts.include) {
opts.include.forEach((route) => {
if (typeof route === "string") {
sitemap.add(route);
return;
}

sitemap.add(route.path, route.options);
})
}

if (opts.exclude) {
opts.exclude.forEach((path) => {
sitemap.remove(path);
})
}


return sitemap.render();
}
}
]
}
}
Loading