Files
RSSHub/website/docs/joinus/advanced/script-standard.md
T
Tony 6d901cb7dc style(eslint): add eslint-unicorn (#14257)
* style: add eslint-unicorn

* style: fix unicorn/no-useless-spread

* style: fix unicorn/no-useless-promise-resolve-reject

* style: fix unicorn/no-for-loop

* fix: codeql bad HTML filtering regexp

* fix: codeql incomplete replace

* fix: unicorn/no-abusive-eslint-disable

* style: fix unicorn/no-new-array

* style: fix unicorn/no-typeof-undefined

* style: fix unicorn/no-zero-fractions

* style: fix unicorn/no-empty-file

* style: fix unicorn/prefer-date-now

* revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js

* style: fix unicorn/prefer-array-find

* style: fix unicorn/prefer-array-flat

* style: fix unicorn/prefer-array-flat-map

* style: fix unicorn/prefer-at

* style: fix unicorn/prefer-string-starts-ends-with

* style: fix unicorn/prefer-includes

* fix: codeql URL substring sanitization

* style: fix unicorn/prefer-optional-catch-binding

* style: fix unicorn/catch-error-name

* style: fix unicorn/escape-case

* style: fix unicorn/prefer-native-coercion-functions

* style: fix unicorn/prefer-regexp-test

* style: fix unicorn/require-array-join-separator

* style: fix unicorn/prefer-math-trunc

* style: fix unicorn/prefer-negative-index

* style: fix unicorn/prefer-dom-node-dataset

* style: fix unicorn/prefer-dom-node-text-content

* style: fix unicorn/prefer-query-selector

* style: fix unicorn/no-array-for-each

* style: fix unicorn/no-negated-condition

* style: fix unicorn/prefer-add-event-listener

* style: fix unicorn/import-style

* style: fix prefer-regex-literals

* style: disable unicorn/no-useless-switch-case

* style: disable unicorn/text-encoding-identifier-case

* style: fix unicorn/prefer-set-has

* style: fix unicorn/prefer-spread

* revert: auto fix on lib/routes/universities/ynnu/edu/base64.js

* style: fix unicorn/no-useless-undefined

* style: fix unicorn/no-array-push-push

* style: fix unicorn/no-useless-undefined again

* style: fix unicorn/no-lonely-if

* style: fix unicorn/prefer-reflect-apply

* style: fix unicorn/switch-case-braces

* style: fix unicorn/prefer-switch

* style: fix unicorn/prefer-array-some

* fix: deepscan UNUSED_VAR_ASSIGN

* style: fix unicorn/prefer-ternary

* fix: follow-up of unicorn/prefer-ternary

* revert: auto fix of unicorn/prefer-string-slice for substring()

* style: disable unicorn/prefer-string-slice

fix: auto fix slice over deprecated substr

* style: fix unicorn/throw-new-error

* style: fix unicorn/filename-case

* test: fix dateParser renaming

* style: fix unicorn/better-regex

* style: fix unicorn/prefer-string-replace-all

* fix(deps): add sanitize-html

* style: fix no-prototype-builtins

* style: fix unicorn/consistent-destructuring

* style: fix unicorn/consistent-function-scoping

* style: fix unicorn/prefer-regexp-test

* style: fix unicorn/prefer-logical-operator-over-ternary

* style: fix unicorn/no-array-callback-reference

* style: add prefer-object-has-own

* style: warn unicorn/no-empty-file

* style: fix unicorn/prefer-number-properties

* style: fix no-useless-undefined again

* style: fix unicorn/numeric-separators-style

* style: disable unicorn/no-array-callback-reference

false postive with cheerio
2024-01-18 20:43:40 +08:00

5.5 KiB

sidebar_position
sidebar_position
2

Script Standard

Code Style

General Guidelines

  • Be consistent!
  • Avoid using deprecated features.
  • Avoid modifying yarn.lock and package.json, unless you are adding a new dependency.
  • Conbine repetitive code into functions.
  • Prefer higher ECMAScript Standard features over lower ones.
  • Sort the entries alphabetically (uppercase first) to make it easier to find an entry.
  • Use HTTPS instead of HTTP whenever possible.
  • Use WebP format instead of JPG whenever possible since it offers better compression.

Formatting

Indentation

  • Use 4 spaces for indentation for consistent and easy-to-read code.

Semicolons

  • Add a semicolon at the end of each statement for improved readability and consistency.

String

  • Use single quotes instead of double quotes whenever possible for consistency and readability.
  • Use template literals over complex string concatenation.
  • Use template literals for GraphQL queries as they make the code more concise and easy to read.

Whitespace

  • Add an empty line at the end of each file.
  • Avoid trailing whitespace for a clean and readable codebase.

Language Features

Casting

  • Avoid re-casting the same type.

Functions

Loops

Variables

  • Use const and let instead of var.
  • Declare one variable per declaration.

Naming

  • Use lowerCamelCase for variables and functions to adhere to standard naming conventions.
  • Use kebab-case for files and folders.
  • Use CONSTANT_CASE for constants.

v2 Route Standard

When creating a new route in RSSHub, you need to organize your files in a specific way. Your namespace folder should be stored in the lib/v2 directory and should include three mandatory files:

  • router.js Registers the routes
  • maintainer.js Provides information about the route maintainer
  • radar.js Provide a RSSHub Radar rule for each route

Your namespace folder structure should look like this:

├───lib/v2
│   ├───furstar
│       ├─── templates
│           ├─── description.art
│       ├─── router.js
│       ├─── maintainer.js
│       ├─── radar.js
│       ├─── someOtherJs.js
│   └───test
│   └───someOtherNamespaces
...

All eligible routes under the lib/v2 path will be automatically loaded without the need for updating the lib/router.js.

Namespace

RSSHub appends the name of all route namespace folders in front of the actual route. Route maintainers should think of the namespace as the root.

Naming Standard

  • Use the second-level domain (SLD) as your namespace. You can find more information about URL structure here.
  • Do not create variations of the same namespace. For more information, see this page

Registering a Route

To register a route, the router.js file should export a method that provides a @koa/router object when initializing the route.

Maintainer List

The maintainer.js file should export an object that provides maintainer information related to the route, including:

  • Key: Corresponding path in the @koa/router object
  • Value: Array of string, including all maintainers' GitHub ID.

To generate a list of maintainers, use the following command: pnpm run build:maintainer, which will create the list under assets/build/.

:::danger

The path in the @koa/router object should be the same as the path in the corresponding documentation before the namespace appended in front of it.

:::

Radar Rules

All routes are required to include the radar.js file, which includes the corresponding domain name. The minimum requirement for a successful match is for the rule to show up on the corresponding site which requires filling in the title and docs fields.

To generate a complete radar-rules.js file, use the following command: yarn build:radar, which will create the file under assets/build/.

:::tip

Remember to remove all build artifacts in assets/build/ before committing.

:::

Rendering Templates

When rendering custom content with HTML, such as item.description, using art-template for layout is mandatory.

All templates should be placed in the namespace's templates folder with the .art file extension.

Example

Here's an example taken from the furstar namespace:

<div>
    <img src="{{ avatar }}" />
    {{ if link !== null }}
    <a href="{{ link }}">{{name}}</a>
    {{ else }}
    <a href="#">{{name}}</a>
    {{ /if }}
</div>
const path = require('path');
const { art } = require('@/utils/render');
const renderAuthor = (author) => art(path.join(__dirname, 'templates/author.art'), author);

v1 Route Standard

:::danger

The v1 Route Standard is deprecated. All new routes should be following the v2 Route Standard.

:::