Skip to content

Commit

Permalink
Merge pull request #20 from kkga/18-add-config-option-for-locale-date
Browse files Browse the repository at this point in the history
use locale date config option
  • Loading branch information
kkga authored Sep 29, 2022
2 parents 6e77e34 + 12ae84a commit 0e47539
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .ter/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"navigation": {
"Usage": "/usage",
"Source code": "https://github.com/kkga/ter"
"Source": "https://github.com/kkga/ter"
},
"author": {
"name": "Gadzhi Kharkharov",
Expand Down
5 changes: 3 additions & 2 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ export async function buildPage(
pagesByTag: taggedPages,
childTags: opts.childTags,
site: opts.userConfig.site,
author: opts.userConfig.author,
navigation: opts.userConfig.navigation,
author: opts.userConfig?.author,
locale: opts.userConfig?.locale,
navigation: opts.userConfig?.navigation,
style: opts.style,
includeRefresh: opts.includeRefresh,
});
Expand Down
6 changes: 4 additions & 2 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export interface UserConfig {
rootCrumb: string;
};
author: { name: string; email: string; url: string };
navigation: Record<string, string>;
navigation?: Record<string, string>;
locale?: {
date?: string;
};
}

export interface BuildConfig {
Expand All @@ -35,7 +38,6 @@ const defaultUserConfig: UserConfig = {
url: "https://example.com/",
rootCrumb: "index",
},
navigation: {},
author: {
name: "Your Name Here",
email: "[email protected]",
Expand Down
41 changes: 29 additions & 12 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,27 @@ deno run -A --unstable https://deno.land/x/ter/main.ts --serve

## Configuration

Before building, Ter checks for configuration file at `.ter/config.json`. If it
doesn't exist, an example configuration file is created before building.
Configuration options can be specified in `.ter/config.json` from the root
directory or in any `json` file specified with `--config` flag when running Ter.

##### Example
If the file does not exist, an example configuration file is created before
building.

### Options

| Key | Description |
| ------------------ | -------------------------------------------------------------- |
| `site.title` | Title of your site. |
| `site.description` | Description of your site. |
| `site.url` | Published URL address of your site. |
| `site.rootCrumb` | Label used for root crumb label (default: "index"). |
| `author.name` | Your name. |
| `author.email` | Your email. |
| `author.url` | Your home page. |
| `navigation` | Optional. Object of navigation items in form of `label: path`. |
| `locale.date` | Optional. Locale used for formatting dates. |

### Example

```json
{
Expand All @@ -105,6 +122,9 @@ doesn't exist, an example configuration file is created before building.
"navigation": {
"about": "/about",
"contact": "/contact"
},
"locale": {
"date": "en-US"
}
}
```
Expand Down Expand Up @@ -133,8 +153,8 @@ content.

Items in the index are sorted in the following order:

1. files with `pinned: true` in the [frontmatter](#frontmatter) are listed at
the top and get an ★ symbol;
1. files with `pinned: true` in the [frontmatter](#markdown-frontmatter) are
listed at the top and get an ★ symbol;
2. directories (child index pages);
3. rest of markdown files, sorted by [date](#dates).

Expand Down Expand Up @@ -193,7 +213,7 @@ skipped during site generation.
### Draft pages

In addition, any markdown file with `draft: true` in the
[frontmatter](#frontmatter) will be ignored.
[frontmatter](#markdown-frontmatter) will be ignored.

### Rendering draft pages

Expand All @@ -213,12 +233,9 @@ after building a site. Here's an example output:

```
[...]
Dead links:
/overview -> /non-existent-page-name
/overview -> /some-dead-link
[...]
```

---
Expand All @@ -238,19 +255,19 @@ If using non-default input and output folders, update the build command
and output directory accordingly.
</details>

##### Build command
#### Build command

```
deno run -A --unstable https://deno.land/x/ter/main.ts
```

##### Output directory
#### Output directory

```
_site
```

##### Install command
#### Install command

```
curl -fsSL https://deno.land/x/install/install.sh | DENO_INSTALL=/usr/local sh
Expand Down
14 changes: 10 additions & 4 deletions serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ const sockets: Set<WebSocket> = new Set();
let servePath: string;

async function watch(opts: WatchOpts) {
const watcher = Deno.watchFs(opts.config.inputPath);
const paths = [opts.config.inputPath, join(Deno.cwd(), ".ter")];
const watcher = Deno.watchFs(paths);
let timer = 0;

const isInOutputDir = (path: string): boolean =>
relative(opts.config.outputPath, path).startsWith("..");

// const isInConfigDir = (path: string): boolean =>
// relative(join(Deno.cwd(), ".ter"), path).startsWith("..") === false;

eventLoop:
for await (const event of watcher) {
if (["any", "access"].includes(event.kind)) {
Expand All @@ -30,15 +37,14 @@ async function watch(opts: WatchOpts) {

for (const eventPath of event.paths) {
if (
eventPath.match(RE_HIDDEN_OR_UNDERSCORED) ||
!relative(opts.config.outputPath, eventPath).startsWith("..")
eventPath.match(RE_HIDDEN_OR_UNDERSCORED) || !isInOutputDir(eventPath)
) {
continue eventLoop;
}
}

console.log(
`>>> ${event.kind}: ${relative(opts.config.inputPath, event.paths[0])}`,
`>>> ${event.kind}: ${relative(Deno.cwd(), event.paths[0])}`,
);
await opts.runner({
config: opts.config,
Expand Down
16 changes: 9 additions & 7 deletions views/base.eta
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
const {
crumbs, childPages, childTags, backlinkPages, pagesByTag,
toc, indexLayout, page, site, includeRefresh, style, navigation,
author
author, locale
} = it;

const { title, description, date, tags, headings, html } = page;
const filteredHeadings = headings && headings.filter(h => h.level >= 2 && h.level <= 5)
const dateOpts = { year: 'numeric', month: 'short', day: 'numeric' };
const datePageOpts = { year: 'numeric', month: 'short', day: 'numeric' };
const dateIndexOpts = { year: 'numeric', month: 'numeric', day: 'numeric' };
%>
<html lang="en">
<head>
Expand Down Expand Up @@ -89,7 +90,7 @@ const dateOpts = { year: 'numeric', month: 'short', day: 'numeric' };
<% if (date || (tags && tags.length > 0)) { %>
<div style="display: flex; gap: 0.5em">
<% if (date) { %>
<time title="Published date" datetime="<%= date %>"><%= date.toLocaleDateString(undefined, dateOpts) %></time>
<time title="Published date" datetime="<%= date %>"><%= date.toLocaleDateString(locale?.date, datePageOpts) %></time>
<% } %>
<% if (tags && tags.length > 0 ) { %>
<% if (date && (tags && tags.length > 0)) { %>
Expand Down Expand Up @@ -147,7 +148,7 @@ const dateOpts = { year: 'numeric', month: 'short', day: 'numeric' };
<h2><a href="<%= url.pathname %>"><%~ title %></a></h2>
<% if (date || (tags && tags.length > 0)) { %>
<div style="display: flex; gap: 0.5em">
<time datetime="<%= date %>"><%= date.toLocaleDateString(undefined, dateOpts) %></time>
<time datetime="<%= date %>"><%= date.toLocaleDateString(locale?.date, dateIndexOpts) %></time>
<% if (tags && tags.length > 0 ) { %>
<% if (date && (tags && tags.length > 0)) { %>
&middot;
Expand Down Expand Up @@ -193,7 +194,7 @@ const dateOpts = { year: 'numeric', month: 'short', day: 'numeric' };
<% if (isIndex) { %>/ ..<% } %>
<span></span>
<% if (date) { %>
<time datetime="<%= date %>"><%= date.toLocaleDateString() %></time>
<time datetime="<%= date %>"><%= date.toLocaleDateString(locale?.date, dateIndexOpts) %></time>
<% } %>
</li>
<% }) %>
Expand All @@ -220,6 +221,7 @@ const dateOpts = { year: 'numeric', month: 'short', day: 'numeric' };
<a href="<%= url.pathname %>"><%= title %></a>
<span></span>
<% if (date) { %>
<time datetime="<%= date %>"><%= date.toLocaleDateString(locale?.date, dateIndexOpts) %></time>
<% } %>
</li>
<% }) %>
Expand All @@ -237,7 +239,7 @@ const dateOpts = { year: 'numeric', month: 'short', day: 'numeric' };
<a href="<%= url.pathname %>"><%= title %></a>
<span></span>
<% if (date) { %>
<time datetime="<%= date %>"><%= date.toLocaleDateString() %></time>
<time datetime="<%= date %>"><%= date.toLocaleDateString(locale?.date, dateIndexOpts) %></time>
<% } %>
</li>
<% }) %>
Expand All @@ -251,7 +253,7 @@ const dateOpts = { year: 'numeric', month: 'short', day: 'numeric' };
Made with <a href="https://github.com/kkga/ter">Ter</a>.
</span>
<a href="/feed.xml">Feed</a>
</footer>
</footer>

</body>
</html>

1 comment on commit 0e47539

@vercel
Copy link

@vercel vercel bot commented on 0e47539 Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ter – ./

ter-git-master-kkga.vercel.app
deno-ter.vercel.app
ter.kkga.me
ter-kkga.vercel.app

Please sign in to comment.