Code Style and Linting
Relevant source files
The following files were used as context for generating this wiki page:This project employs a strict set of tools and practices to ensure code consistency, quality, and maintainability. The strategy covers both the JavaScript source code and the generated XML output for the RSS feed. For JavaScript, the project relies on the standard package for zero-configuration linting and formatting. For the RSS feed, custom scripts are used to format, validate, and sanitize the XML content, ensuring it is well-formed and safe for consumers.
The contribution guidelines outlined in the project expect adherence to these standards, ensuring that all code, whether from core developers or external contributors, maintains a uniform style and quality. This dual focus on source code and output data integrity is crucial for the project's reliability.
Sources: package.json, README.md, CONTRIBUTING.md
JavaScript Code Style and Linting
The project uses StandardJS to enforce a consistent JavaScript style. This approach avoids debates over style by using a single, opinionated tool for checking and formatting code.
Sources: package.json:36-38
Tooling
The primary tool for JavaScript code quality is the standard package. It is listed as a development dependency in the project's configuration.
"devDependencies": {
"standard": "17.1.2"
}
Sources: package.json:36-38
StandardJS is a zero-configuration tool, meaning there are no .eslintrc, .jshintrc, or other configuration files. The rules are built into the tool itself, simplifying the setup and ensuring consistency across all development environments.
NPM Scripts
The package.json file defines two main scripts for developers to interact with the linter.
| Script | Command | Description |
|---|---|---|
lint | standard | Checks all JavaScript files in the project for style violations and errors. |
lint:fix | standard --fix | Automatically fixes most stylistic issues found in the JavaScript files. |
Sources: package.json:13-14, README.md:46-49
Developer Workflow
The typical workflow for a developer is to run the linter to check for issues and then run the fixer to resolve them automatically before committing code.
The following diagram illustrates the developer linting workflow.
Sources: package.json:13-14
RSS Feed Formatting and Validation
Beyond source code, the project also enforces a clean and valid format for its primary output: the feed.xml file. This is handled by a combination of formatting, sanitization, and validation scripts.
XML Formatting
The project uses the xml-formatter package to ensure the feed.xml is consistently indented and structured. The scripts/format.js script is responsible for this process.
The formatting process involves:
- Reading the current
feed.xmlcontent. - Parsing the XML into a JavaScript object using
rss-parser. - Filtering and sorting the feed items by date.
- Recomposing the feed items into an XML string.
- Formatting the final XML string using
xml-formatterwith a 2-space indentation. - Overwriting the
feed.xmlfile with the formatted content.
Sources: scripts/format.js:1-42
This diagram shows the flow of the RSS formatting script.
Sources: scripts/format.js, package.json:10
Content Sanitization Utilities
To prevent malformed or unsafe content from being injected into the RSS feed, the project uses a suite of utility functions defined in utils/index.js. These functions are used by the data collection scripts (collect-*.js) before content is added to the feed.
The main functions are:
| Function | Description |
|---|---|
escapeXmlCharacters(text) | Escapes special XML characters like &, <, >, ", '. |
stripHtmlTags(text) | Removes all HTML tags from a string. |
escapeHtmlTags(text) | Converts HTML tag brackets (<, >) to their entity equivalents (<, >). |
cleancontent(text, options) | An orchestrator function that removes blacklisted keywords from text. |
filterFeedItems(items, options) | Filters a list of feed items to remove incomplete entries and duplicates based on guid, link, or title. |
These functions are thoroughly tested in utils/__tests__/utils.test.js to ensure they handle various inputs correctly, including edge cases and malformed strings.
Sources: utils/index.js:27-113, 203-217, utils/tests/utils.test.js
The following diagram illustrates how content is cleaned before being composed into a feed item.
RSS Feed Validation
To ensure the final feed.xml adheres to the RSS standard, a validation script is provided. The npm run rss:validate command executes scripts/validate.js, which sends the feed.xml content to the W3C Feed Validation Service.
The script parses the HTML response from the validator to determine if the feed is valid. If the response contains "Sorry", it indicates an invalid feed, and the script logs an error. Otherwise, it confirms the feed's validity.
Sources: scripts/validate.js:1-29, package.json:8
Conclusion
The nodejs-news-feeder project maintains a high standard of quality for both its source code and its data outputs. It uses StandardJS for effortless JavaScript linting and formatting, ensuring code consistency. For its RSS feed, it employs a multi-stage process of content sanitization, duplicate filtering, standardized XML formatting, and W3C validation. This comprehensive approach to code style and data integrity ensures the project is both maintainable for developers and reliable for its end-users.