-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove `ember-prism` * Fix tests * Fix tests
- Loading branch information
Showing
11 changed files
with
2,496 additions
and
2,393 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<pre | ||
class="{{this.languageClass}} {{if @showLineNumbers "line-numbers"}}" | ||
data-start={{@start}} | ||
> | ||
{{~! ~}} | ||
<CodeInline | ||
...attributes | ||
@code={{@code}} | ||
@language={{@language}} | ||
/> | ||
{{~! ~}} | ||
</pre> |
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,27 @@ | ||
import Component from '@glimmer/component'; | ||
|
||
interface CodeBlockSignature { | ||
Element: HTMLElement; | ||
Args: { | ||
code: string; | ||
language?: string; | ||
showLineNumbers?: boolean; | ||
start?: string; | ||
}; | ||
} | ||
|
||
export default class CodeBlock extends Component<CodeBlockSignature> { | ||
get language() { | ||
return this.args.language ?? 'markup'; | ||
} | ||
|
||
get languageClass() { | ||
return `language-${this.language}`; | ||
} | ||
} | ||
|
||
declare module '@glint/environment-ember-loose/registry' { | ||
export default interface Registry { | ||
CodeBlock: typeof CodeBlock; | ||
} | ||
} |
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,7 @@ | ||
<code | ||
...attributes | ||
class="{{this.languageClass}}" | ||
{{this.setPrismCode}} | ||
> | ||
{{~! ~}}{{this.prismCode}}{{~! ~}} | ||
</code> |
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,67 @@ | ||
import Component from '@glimmer/component'; | ||
import { modifier } from 'ember-modifier'; | ||
import { htmlSafe, type SafeString } from '@ember/template'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { assert } from '@ember/debug'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
declare const Prism: any; | ||
|
||
interface CodeInlineSignature { | ||
Element: HTMLElement; | ||
Args: { | ||
code: string; | ||
language?: string; | ||
}; | ||
} | ||
|
||
export default class CodeInline extends Component<CodeInlineSignature> { | ||
@tracked prismCode: string | SafeString = ''; | ||
|
||
get code() { | ||
const code = this.args.code; | ||
|
||
assert( | ||
"ember-prism's <CodeBlock/> and <CodeInline/> components require a `code` parameter to be passed in.", | ||
code !== undefined, | ||
); | ||
if (Prism?.plugins?.NormalizeWhitespace) { | ||
return Prism.plugins.NormalizeWhitespace.normalize(code); | ||
} | ||
|
||
return code; | ||
} | ||
|
||
get language() { | ||
return this.args.language ?? 'markup'; | ||
} | ||
|
||
get languageClass() { | ||
return `language-${this.language}`; | ||
} | ||
|
||
setPrismCode = modifier((element: Element) => { | ||
const code = this.code; | ||
const language = this.language; | ||
const grammar = Prism.languages[language]; | ||
|
||
if (code && language && grammar) { | ||
this.prismCode = htmlSafe(Prism.highlight(code, grammar, language)); | ||
} else { | ||
this.prismCode = ''; | ||
} | ||
|
||
// Force plugin initialization, required for Prism.highlight usage. | ||
// See https://github.com/PrismJS/prism/issues/1234 | ||
Prism.hooks.run('complete', { | ||
code, | ||
element, | ||
}); | ||
}); | ||
} | ||
|
||
declare module '@glint/environment-ember-loose/registry' { | ||
export default interface Registry { | ||
CodeInline: typeof CodeInline; | ||
} | ||
} |
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,19 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'docs/tests/helpers'; | ||
import { render } from '@ember/test-helpers'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
|
||
module('Integration | Component | code-block', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders', async function (assert) { | ||
// Set any properties with this.set('myProperty', 'value'); | ||
// Handle any actions with this.set('myAction', function(val) { ... }); | ||
|
||
await render( | ||
hbs`<CodeBlock @language="js" @code="console.log('hello');" />`, | ||
); | ||
|
||
assert.dom().hasText("console.log('hello');"); | ||
}); | ||
}); |
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,19 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'docs/tests/helpers'; | ||
import { render } from '@ember/test-helpers'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
|
||
module('Integration | Component | code-inline', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders', async function (assert) { | ||
// Set any properties with this.set('myProperty', 'value'); | ||
// Handle any actions with this.set('myAction', function(val) { ... }); | ||
|
||
await render( | ||
hbs`<CodeInline @language="js" @code="console.log('hello');" />`, | ||
); | ||
|
||
assert.dom().hasText("console.log('hello');"); | ||
}); | ||
}); |
Oops, something went wrong.