Skip to content

Commit

Permalink
Merge pull request #3 from jackdbd/text-to-speech
Browse files Browse the repository at this point in the history
Eleventy plugin for the Google Cloud Text-to-Speech API
  • Loading branch information
jackdbd authored Jul 5, 2022
2 parents 8f2c984 + a0df0ae commit 9e6ba0c
Show file tree
Hide file tree
Showing 28 changed files with 2,892 additions and 92 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ jobs:

- name: 🔍 Test all libraries
env:
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
TELEGRAM: ${{ secrets.TELEGRAM }}
run: npm run test:ci
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:

- name: 🔍 Test all libraries
env:
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
TELEGRAM: ${{ secrets.TELEGRAM }}
run: npm run test:ci

Expand Down
5 changes: 4 additions & 1 deletion config/jest.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ const project = (package_name) => {
}

// https://jestjs.io/docs/configuration#projects-arraystring--projectconfig
const projects = [project('eleventy-telegram-plugin')]
const projects = [
project('eleventy-plugin-text-to-speech'),
project('eleventy-telegram-plugin')
]

const config = {
projects,
Expand Down
2,216 changes: 2,128 additions & 88 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@types/debug": "^4.1.7",
"@types/html-to-text": "^8.1.0",
"@types/jsdom": "^16.2.14",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"debug": "^4.3.4",
"eslint": "^8.17.0",
Expand Down
31 changes: 31 additions & 0 deletions packages/demo-site/config/eleventy.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const path = require('node:path')
const slugify = require('slugify')
const { telegramPlugin } = require('@jackdbd/eleventy-telegram-plugin')
const {
textToSpeechPlugin
} = require('@jackdbd/eleventy-plugin-text-to-speech')

const ROOT = path.join(__filename, '..', '..')
const OUTPUT_DIR = path.join(ROOT, '_site')
Expand All @@ -10,12 +14,39 @@ const OUTPUT_DIR = path.join(ROOT, '_site')
module.exports = function (eleventyConfig) {
const { chat_id: chatId, token } = JSON.parse(process.env.TELEGRAM)

eleventyConfig.addFilter('slugify', function (str) {
return slugify(str, {
lower: true,
remove: /[*+~.·,()'"`´%!?¿:@]/g,
replacement: '-'
})
})

// https://www.11ty.dev/docs/collections/
eleventyConfig.addCollection('pages-with-audio', (collectionApi) => {
// const all_items = collectionApi.getAll()
const items = collectionApi.getFilteredByGlob([
'**/pages/*.njk',
'**/posts/*.md'
])
// console.log('🚀 ~ items', items)
return items
})

eleventyConfig.addPlugin(telegramPlugin, {
chatId,
token,
textBeforeBuild: `<i>demo-site</i> build <b>START</b>`
})

eleventyConfig.addPlugin(textToSpeechPlugin, {
// audioEncoding: 'MP3',
audioEncoding: 'LINEAR16',
regexPattern: '.*/posts/.*.html$',
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
voice: { languageCode: 'en-GB', name: 'en-GB-Wavenet-C' }
})

// https://www.11ty.dev/docs/config/#configuration-options
return {
dataTemplateEngine: 'njk',
Expand Down
5 changes: 3 additions & 2 deletions packages/demo-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"node": ">=16.0.0"
},
"scripts": {
"build": "ELEVENTY_ENV=production TELEGRAM=$(cat ../../secrets/telegram.json) eleventy --config config/eleventy.cjs",
"build": "ELEVENTY_ENV=production GOOGLE_APPLICATION_CREDENTIALS=~/repos/undici/secrets/sa-wasm-news.json TELEGRAM=$(cat ../../secrets/telegram.json) eleventy --config config/eleventy.cjs",
"build:watch": "ELEVENTY_ENV=production TELEGRAM=$(cat ../../secrets/telegram.json) eleventy --config config/eleventy.cjs --watch",
"clean": "rimraf _site/",
"lint": "eslint --config ../../config/eslint.cjs",
Expand All @@ -31,6 +31,7 @@
"peerDependencies": {},
"devDependencies": {
"@11ty/eleventy": "^1.0.1",
"@jackdbd/eleventy-telegram-plugin": "*"
"@jackdbd/eleventy-telegram-plugin": "*",
"@jackdbd/eleventy-plugin-text-to-speech": "*"
}
}
17 changes: 17 additions & 0 deletions packages/demo-site/src/includes/components/post-list.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<ul class="post-list">
{% for post in posts | reverse %}

<li>
<h2>
<a href="{{ post.url | url }}">
{% if post.data.title %}
{{ post.data.title }}
{% else %}
Untitled post
{% endif %}
</a>
</h2>
</li>

{% endfor %}
</ul>
15 changes: 15 additions & 0 deletions packages/demo-site/src/layouts/page.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
layout: bare-minimum.njk
---
{% block content %}

<main class="wrapper">
<div>
<h1>{{ title }}</h1>
{% block pagecontent %}
{{ content | safe }}
{% endblock pagecontent %}
</div>
</main>

{% endblock content %}
14 changes: 14 additions & 0 deletions packages/demo-site/src/layouts/post.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
layout: bare-minimum.njk
section: post
---
{% block content %}

<main>
<article>
<h1>{{ title }}</h1>
{{ content | safe }}
</article>
</main>

{% endblock content %}
10 changes: 10 additions & 0 deletions packages/demo-site/src/pages/404.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
eleventyExcludeFromCollections: true
layout: page.njk
permalink: 404.html
title: Oops! Not Found (404)
---
{% block pagecontent %}
<p>This is not the page you were looking for.</p>
<p>Otherwise simply go back to the <a href="{{ '/' | url }}">home</a>.</p>
{% endblock pagecontent %}
11 changes: 11 additions & 0 deletions packages/demo-site/src/pages/home.njk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ title: Hello world!
<main class="wrapper">
<h1>{{ title }}</h1>
<p>This is a demo site</p>

<p>posts</p>
{% set posts = collections.post | reverse %}
{% include "components/post-list.njk" %}

<p>pages with audio</p>
<ul>
{%- for item in collections['pages-with-audio'] -%}
<li>{{ item.data.title }}</li>
{%- endfor -%}
</ul>
</main>

{% endblock content %}
8 changes: 8 additions & 0 deletions packages/demo-site/src/posts/capybaras-are-cool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Capybaras are cool
tags:
- animal
- capybara
- cool
---
This is just a short blog post to remember that [capybaras](https://en.wikipedia.org/wiki/Capybara) are cool.
8 changes: 8 additions & 0 deletions packages/demo-site/src/posts/posts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"layout": "post.njk",
"ogp": {
"type": "article"
},
"permalink": "posts/{{ title | slugify }}/index.html",
"tags": ["post"]
}
21 changes: 21 additions & 0 deletions packages/eleventy-plugin-text-to-speech/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Giacomo Debidda

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
58 changes: 58 additions & 0 deletions packages/eleventy-plugin-text-to-speech/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# @jackdbd/eleventy-plugin-text-to-speech

[![npm version](https://badge.fury.io/js/@jackdbd%2Feleventy-plugin-text-to-speech.svg)](https://badge.fury.io/js/@jackdbd%2Feleventy-plugin-text-to-speech)
![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@jackdbd%2Feleventy-plugin-text-to-speech)

Eleventy plugin that send messages to a Telegram chat of your choice.

## Installation

```sh
npm install --save-dev @jackdbd/eleventy-plugin-text-to-speech
```

Before you can begin using the Text-to-Speech API, you must enable it. Using Cloud Shell, you can enable the API with the following command:

```sh
gcloud services enable texttospeech.googleapis.com
```

Create a service account that can use the Text-to-Speech API.

```sh
gcloud iam service-accounts create sa-text-to-speech-user \
--display-name "Text-to-Speech user SA"
```

## Usage

```js
const { textToSpeechPlugin } = require('@jackdbd/eleventy-plugin-text-to-speech')

module.exports = function (eleventyConfig) {
// some eleventy configuration...

eleventyConfig.addPlugin(textToSpeechPlugin, {
audioEncoding: 'OGG_OPUS',
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
regexPattern: '.*/posts/.*.html$',
voice: { languageCode: 'en-GB', name: 'en-GB-Wavenet-C' }
})

// some more eleventy configuration...
}
```

## Configuration

### Options

| Option | Default | Explanation |
| --- | --- | --- |
| `audioEncoding` | `OGG_OPUS` | Encoding for the audio file. It must be one of [supported audio encodings](https://cloud.google.com/speech-to-text/docs/encoding#audio-encodings) of the Cloud Text-to-Speech API. |
| `regexPattern` | `.*.html$` | regex pattern to find text files to convert into speech. |
| `keyFilename` | `process.env.GOOGLE_APPLICATION_CREDENTIALS` | credentials for the Cloud Text-to-Speech API. |
| `voiceLanguageCode` | `en-US` | Name of the Text-to-Speech languageCode to use. [See list here](https://cloud.google.com/text-to-speech/docs/voices). |
| `voiceName` | `en-US-Standard-J` | Name of the Text-to-Speech voice to use. [See list here](https://cloud.google.com/text-to-speech/docs/voices). |

> :warning: Don't forget to set either `keyFilename` or the `GOOGLE_APPLICATION_CREDENTIALS` environment variable on your build server. For example, if your build runs on the Github CI, use [GitHub secrets](https://docs.github.com/en/actions/reference/encrypted-secrets); if the build runs on Netlify, use [Build environment variables](https://docs.netlify.com/configure-builds/environment-variables/).
66 changes: 66 additions & 0 deletions packages/eleventy-plugin-text-to-speech/__tests__/index.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const fs = require('node:fs')
const path = require('node:path')
const fsPromises = require('fs/promises')
const Eleventy = require('@11ty/eleventy/src/Eleventy')
const { textToSpeechPlugin } = require('../lib/index.js')

const PACKAGE_ROOT = path.join(__filename, '..', '..')
const OUTPUT_DIR = path.join(PACKAGE_ROOT, '_site')

describe('textToSpeechPlugin', () => {
beforeAll(async () => {
if (!fs.existsSync(OUTPUT_DIR)) {
await fsPromises.mkdir(OUTPUT_DIR)
}
})

afterAll(async () => {
if (fs.existsSync(OUTPUT_DIR)) {
await fsPromises.rmdir(OUTPUT_DIR)
}
})

let eleventyConfig
beforeEach(() => {
const eleventy = new Eleventy()
eleventyConfig = eleventy.eleventyConfig.userConfig
})

it('throws when passed an `audioEncoding` unsupported by the Cloud Text-to-Speech API', () => {
const userConfig = {
audioEncoding: 'foo'
}

expect(() => {
textToSpeechPlugin(eleventyConfig, userConfig)
}).toThrowError('"audioEncoding" must be one of')
})

it('allows `audioEncoding` to be `MP3`', () => {
const userConfig = {
audioEncoding: 'MP3'
}

eleventyConfig.dir = {
output: OUTPUT_DIR
}

expect(() => {
textToSpeechPlugin(eleventyConfig, userConfig)
}).not.toThrow()
})

it('allows `audioEncoding` to be `OGG_OPUS`', () => {
const userConfig = {
audioEncoding: 'OGG_OPUS'
}

eleventyConfig.dir = {
output: OUTPUT_DIR
}

expect(() => {
textToSpeechPlugin(eleventyConfig, userConfig)
}).not.toThrow()
})
})
Loading

0 comments on commit 9e6ba0c

Please sign in to comment.