-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jackdbd/text-to-speech
Eleventy plugin for the Google Cloud Text-to-Speech API
- Loading branch information
Showing
28 changed files
with
2,892 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
packages/eleventy-plugin-text-to-speech/__tests__/index.test.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
Oops, something went wrong.