Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robot-inventor/rehype-auto-ads
rehype.js plugin that automatically inserts Google Adsense (and theoretically any ad service) code
https://github.com/robot-inventor/rehype-auto-ads
rehype rehype-plugin unified
Last synced: about 1 month ago
JSON representation
rehype.js plugin that automatically inserts Google Adsense (and theoretically any ad service) code
- Host: GitHub
- URL: https://github.com/robot-inventor/rehype-auto-ads
- Owner: Robot-Inventor
- License: mit
- Created: 2024-03-05T06:41:56.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-10-03T01:29:18.000Z (about 2 months ago)
- Last Synced: 2024-10-08T13:51:57.728Z (about 1 month ago)
- Topics: rehype, rehype-plugin, unified
- Language: TypeScript
- Homepage:
- Size: 232 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# rehype-auto-ads
rehype.js plugin that automatically inserts Google Adsense (and theoretically any ad service) code.
This plugin inserts an ad code for each specified number of paragraphs. For example, insert Google Adsense display ad code every 5 paragraphs.
(Unlike Google Adsense's automatic ads) no ad code is inserted into blockquote or list items!
## Install
```bash
npm install rehype-auto-ads
```## Usage
```javascript
import remarkParse from "remark-parse";
import rehypeStringify from "rehype-stringify";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
import rehypeAutoAds from "rehype-auto-ads";const options = {
adCode: "",
paragraphInterval: 2
};const processor = unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeAutoAds, options)
.use(rehypeStringify);const markdown = `
# Hello, world!This is a paragraph.
This is a paragraph.
This is a paragraph.
This is a paragraph.
`;processor.process(markdown).then((result) => {
console.log(result.toString());
});
```The above code will output the following:
```markdown
Hello, world!
This is a paragraph.
This is a paragraph.
This is a paragraph.
This is a paragraph.
```## Options
```typescript
export interface RehypeAutoAdsOptions {
adCode: string;
countFrom?: number;
paragraphInterval?: number;
shouldInsertAd?: (
vfile: VFile,
previousNode: Root | ElementContent | Doctype,
nextNode: Root | ElementContent | Doctype | null,
ancestors: (Root | Element)[]
) => boolean;
}
```### ``adCode``
The ad code to be inserted. For example, Google Adsense display ad code.
### ``countFrom``
Initial value of paragraph counter. In other words, this value should be set to the value of ``paragraphInterval`` minus the number of paragraphs you want to insert the first ad.
If you want to insert ad code from the third paragraph and every 5 paragraphs, set this to ``2``.
Default: ``0``
### ``paragraphInterval``
The value indicating how many paragraphs to insert advertising code. For example, specifying 5 will insert ads every 5 paragraphs.
Default: ``5``
### ``shouldInsertAd``
Function to determine whether to insert an ad code. If this function returns ``true``, the ad code will be inserted. The default implementation always returns ``true``.
#### Parameters
- ``vfile``: vfile of the current file.
- ``previousNode``: The previous node of the insertion point.
- ``nextNode``: The next node of the insertion point.
- ``ancestors``: Ancestors of the `previousNode`.## Development
```bash
npm install
```### Build
```bash
npm run build
```### Format and Lint
```bash
npm run format
npm run lint
```### Test
```bash
npm run test
```### Pull Requests
This repository uses [Changesets](https://github.com/changesets/changesets) to manage versioning and releases. When creating a pull request, please run the Changesets CLI and commit the changeset file.
```bash
npx changeset
```