Skip to content

Commit

Permalink
Merge pull request #8 from IgnaceMaes/configure-options
Browse files Browse the repository at this point in the history
feat: configure options for theme and languages
  • Loading branch information
IgnaceMaes authored Jan 24, 2024
2 parents dceafa4 + 40486cc commit 1f59ec0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-lemons-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ember-showdown-shikiji": minor
---

feat: add theme and language config options
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ module.exports = function () {
};
```

### Configuration

In `config/environment.js` you can configure the following options:

```js
module.exports = function (environment) {
const ENV = {
// Other options ...
'ember-showdown-shikiji': {
theme: 'github-dark', // The theme to use for highlighting
languages: ['javascript', 'handlebars'], // The languages to highlight
},
};
return ENV;
};
```

By default the theme used is `dark-plus` and all languages are loaded.

## Contributing

See the [Contributing](CONTRIBUTING.md) guide for details.
Expand Down
18 changes: 12 additions & 6 deletions ember-showdown-shikiji/src/initializers/showdown-shikiji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { glimmerHandlebarsGrammar } from '../glimmer-handlebars-grammar.ts';
const CODE_BLOCK_REGEX =
/(?:^|\n)(?: {0,3})(```+|~~~+)(?: *)([^\n`~]*)\n([\s\S]*?)\n(?: {0,3})\1/g;

async function initializeShikiji() {
async function initializeShikiji(theme: string, languages: string[]) {
const highlighter = await getHighlighter({
themes: ['dark-plus'],
langs: [glimmerHandlebarsGrammar, ...Object.keys(bundledLanguages)],
themes: [theme],
langs: [glimmerHandlebarsGrammar, ...languages],
});

return highlighter;
Expand Down Expand Up @@ -86,7 +86,7 @@ function transformCodeBlock(
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace

const { language, attributes } = extractCodeBlockHeader(languageBlock);
const shikijiLanguage = Object.keys(bundledLanguages).includes(language)
const shikijiLanguage = highlighter.getLoadedLanguages().includes(language)
? language
: 'text';

Expand All @@ -97,7 +97,7 @@ function transformCodeBlock(

codeblock = highlighter.codeToHtml(codeblock, {
lang: shikijiLanguage,
theme: 'dark-plus',
theme: highlighter.getLoadedThemes()[0]!,
transformers: [transformerNotationDiff()],
});
codeblock = codeblock.replace(
Expand Down Expand Up @@ -132,7 +132,13 @@ function transformCodeBlock(

export async function initialize(application: Application) {
application.deferReadiness();
const highlighter = await initializeShikiji();

const config = application.resolveRegistration('config:environment') as {
'ember-showdown-shikiji'?: { theme?: string; languages?: string[] };
};
const { theme = 'dark-plus', languages = Object.keys(bundledLanguages) } =
config['ember-showdown-shikiji'] ?? {};
const highlighter = await initializeShikiji(theme, languages);

showdown.subParser('githubCodeBlocks', function (text, options, globals) {
// Early exit if option is not enabled
Expand Down

0 comments on commit 1f59ec0

Please sign in to comment.