Skip to content

Commit

Permalink
refactor: ts-check all utils
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Mar 26, 2019
1 parent 3478215 commit 06b12d1
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 22 deletions.
5 changes: 5 additions & 0 deletions packages/saber/lib/utils/getPageType.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// @ts-check
/**
* @param {string} relative - The relative path of a file
* @param {string} slug - The page slug
*/
module.exports = (relative, slug) => {
if (relative.startsWith('_posts/')) {
return 'post'
Expand Down
26 changes: 20 additions & 6 deletions packages/saber/lib/utils/getPermalink.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
// @ts-check

// Default:
// about.md => /about.html
// about/index.md => /about
// index.md => /

/**
* Get the permalink of a page
* @param {object} attributes - Page attributes
* @param {string} attributes.slug - The page slug
* @param {string} attributes.type - The page type
* @param {Date} attributes.createdAt - The creation time
* @param {import('saber').Permalinks} permalinks - The `permalinks` config option
*/
module.exports = (attributes, permalinks) => {
if (attributes.slug === 'index') return '/'

Expand All @@ -15,18 +25,22 @@ module.exports = (attributes, permalinks) => {
)

const permalinkTemplate = permalinks[attributes.type] || permalinks.page
const date = new Date(attributes.date || attributes.createdAt)
const date = new Date(attributes.createdAt)
return permalinkTemplate
.replace(/:slug/, attributes.slug)
.replace(/:year/, () => date.getFullYear())
.replace(/:month/, () => padZero(date.getMonth() + 1))
.replace(/:i_month/, () => date.getMonth() + 1)
.replace(/:day/, () => padZero(date.getDate()))
.replace(/:i_day/, () => date.getDate())
.replace(/:year/, () => `${date.getFullYear()}`)
.replace(/:month/, () => `${padZero(date.getMonth() + 1)}`)
.replace(/:i_month/, () => `${date.getMonth() + 1}`)
.replace(/:day/, () => `${padZero(date.getDate())}`)
.replace(/:i_day/, () => `${date.getDate()}`)
.replace(/^\/index.html$/, '/')
.replace(/\/index\.html$/, '/')
}

/**
* Left-pad '0' to number that is smaller than 10
* @param {number} input
*/
function padZero(input) {
return input < 10 ? `0${input}` : input
}
7 changes: 6 additions & 1 deletion packages/saber/lib/utils/slash.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
// Convert back slash to slash
// @ts-check

/**
* Convert back slash to slash
* @param {string} input
*/
module.exports = input => input && input.replace(/\\/g, '/')
3 changes: 2 additions & 1 deletion packages/saber/lib/utils/spinner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const ora = require('ora')
// @ts-check
const ora = require('ora').default

module.exports = ora()
5 changes: 5 additions & 0 deletions packages/saber/lib/utils/validateConfig.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// @ts-check
const { struct } = require('superstruct')

/**
* Validate saber config
* @param {any} config
*/
module.exports = config => {
const siteConfig = struct.interface(
{
Expand Down
6 changes: 4 additions & 2 deletions packages/saber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
"bin",
"lib",
"!**/*.test.js",
"!**/__*__/**"
"!**/__*__/**",
"/types"
],
"scripts": {
"test": "jest --env node"
},
"main": "lib/index.js",
"bin": "lib/cli.js",
"types": "types/index.d.ts",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.2.2",
Expand All @@ -33,7 +35,7 @@
"joycon": "^2.2.3",
"lodash.merge": "^4.6.1",
"mini-css-extract-plugin": "^0.5.0",
"ora": "^3.0.0",
"ora": "^3.2.0",
"polka": "^0.5.1",
"pretty-ms": "^4.0.0",
"resolve-from": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/saber/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"esModuleInterop": true,
"moduleResolution": "node"
},
"include": ["types.d.ts", "lib/**/*"]
"include": ["lib/**/*"]
}
Empty file removed packages/saber/types.d.ts
Empty file.
89 changes: 89 additions & 0 deletions packages/saber/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
declare module 'saber' {
interface Config {
siteConfig?: {
[k: string]: any
}

themeConfig?: {
[k: string]: any
}

permalinks?: Permalinks | ((page: Page) => Permalinks)

build?: {
/**
* The base URL your application will be deployed at.
* If your website is located at a sub directory, e.g. `https://example.com/blog`, you should set this option to `/blog/` (trailing slash is optional).
*/
publicUrl?: string
}

plugins?: Plugin[]

markdown?: {
/**
* The path to a module or npm package name that slugifies the markdown headers.
* The module should have following signature:
* ```js
* type Slugify = (header: string) => string
* ```
* You can use the package `limax` which provides CJK support.
*/
slugify?: string
/**
* The path to a file or npm package name that highlights code blocks in markdown.
* `saber-highlighter-` prefix is optional.
* Note that a highlighter will only tokenize the code, you need to add corresponding CSS yourself.
*/
highlighter?: string
/**
* Options for markdown-it
*/
options?: any
plugins?: MarkdownItPlugin[]
}
}

interface Permalinks {
[pageType: string]: string
}

interface Page {
attributes: {
type: string
layout?: string
createdAt: Date
updatedAt: Date
permalink: string
slug: string
[k: string]: any
}
internal: {
id: string
isFile?: boolean
relative?: string
absolute?: string
saved?: boolean
}
content?: string
contentType?: string
}

type Plugin =
| string
| {
/** The path to your plugin or an npm package name */
resolve: string
/** Plugin options */
options?: object
}

interface MarkdownItPlugin {
// A package name or relative path
// e.g. markdown-it-footnote
resolve: string
options?: any
}

export { Config, Permalinks }
}
22 changes: 11 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2473,10 +2473,10 @@ cli-cursor@^2.0.0, cli-cursor@^2.1.0:
dependencies:
restore-cursor "^2.0.0"

cli-spinners@^1.1.0:
version "1.3.1"
resolved "https://registry.npmjs.org/cli-spinners/-/cli-spinners-1.3.1.tgz#002c1990912d0d59580c93bd36c056de99e4259a"
integrity sha512-1QL4544moEsDVH9T/l6Cemov/37iv1RtoKf7NJ04A60+4MREXNfx/QvavbH6QoGdsD4N4Mwy49cmaINR/o2mdg==
cli-spinners@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.0.0.tgz#4b078756fc17a8f72043fdc9f1f14bf4fa87e2df"
integrity sha512-yiEBmhaKPPeBj7wWm4GEdtPZK940p9pl3EANIrnJ3JnvWyrPjcFcsEq6qRUuQ7fzB0+Y82ld3p6B34xo95foWw==

cli-truncate@^0.2.1:
version "0.2.1"
Expand Down Expand Up @@ -7610,16 +7610,16 @@ optionator@^0.8.1, optionator@^0.8.2:
type-check "~0.3.2"
wordwrap "~1.0.0"

ora@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/ora/-/ora-3.0.0.tgz#8179e3525b9aafd99242d63cc206fd64732741d0"
integrity sha512-LBS97LFe2RV6GJmXBi6OKcETKyklHNMV0xw7BtsVn2MlsgsydyZetSCbCANr+PFLmDyv4KV88nn0eCKza665Mg==
ora@^3.2.0:
version "3.2.0"
resolved "https://registry.npmjs.org/ora/-/ora-3.2.0.tgz#67e98a7e11f7f0ac95deaaaf11bb04de3d09e481"
integrity sha512-XHMZA5WieCbtg+tu0uPF8CjvwQdNzKCX6BVh3N6GFsEXH40mTk5dsw/ya1lBTUGJslcEFJFQ8cBhOgkkZXQtMA==
dependencies:
chalk "^2.3.1"
chalk "^2.4.2"
cli-cursor "^2.1.0"
cli-spinners "^1.1.0"
cli-spinners "^2.0.0"
log-symbols "^2.2.0"
strip-ansi "^4.0.0"
strip-ansi "^5.0.0"
wcwidth "^1.0.1"

os-browserify@^0.3.0:
Expand Down

0 comments on commit 06b12d1

Please sign in to comment.