feat(docs-infra): add support for unformatted code-snippets (#41335)
This commit adds support for skipping formatting in `<aio-code>` elements (which are used by `<code-example>` and `<code-pane>` elements under the hood) by specifying the `language` option as `'root'`. This is useful for code-snippets that include plain text. PR Close #41335
This commit is contained in:
parent
5c9bd237e3
commit
a4e3ea0311
|
@ -117,6 +117,15 @@ describe('CodeComponent', () => {
|
|||
expect(getFormattedCode()).toBe(
|
||||
`Formatted code (language: auto, linenums: true): ${bigMultiLineCode}`);
|
||||
});
|
||||
|
||||
it('should skip prettify if language is `\'none\'`', () => {
|
||||
hostComponent.setCode(bigMultiLineCode);
|
||||
hostComponent.language = 'none';
|
||||
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(getFormattedCode()).toBe(bigMultiLineCode);
|
||||
});
|
||||
});
|
||||
|
||||
describe('whitespace handling', () => {
|
||||
|
|
|
@ -3,6 +3,7 @@ import { Clipboard } from '@angular/cdk/clipboard';
|
|||
import { Logger } from 'app/shared/logger.service';
|
||||
import { PrettyPrinter } from './pretty-printer.service';
|
||||
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
|
||||
/**
|
||||
|
@ -108,15 +109,22 @@ export class CodeComponent implements OnChanges {
|
|||
}
|
||||
|
||||
private formatDisplayedCode() {
|
||||
const linenums = this.getLinenums();
|
||||
const leftAlignedCode = leftAlign(this.code);
|
||||
this.setCodeHtml(leftAlignedCode); // start with unformatted code
|
||||
this.codeText = this.getCodeText(); // store the unformatted code as text (for copying)
|
||||
|
||||
this.pretty
|
||||
.formatCode(leftAlignedCode, this.language, this.getLinenums())
|
||||
.pipe(tap(() => this.codeFormatted.emit()))
|
||||
.subscribe(c => this.setCodeHtml(c), () => { /* ignore failure to format */ }
|
||||
);
|
||||
const skipPrettify = of(undefined);
|
||||
const prettifyCode = this.pretty
|
||||
.formatCode(leftAlignedCode, this.language, linenums)
|
||||
.pipe(tap(formattedCode => this.setCodeHtml(formattedCode)));
|
||||
|
||||
if (linenums !== false && this.language === 'none') {
|
||||
this.logger.warn(`Using 'linenums' with 'language: none' is currently not supported.`);
|
||||
}
|
||||
|
||||
((this.language === 'none' ? skipPrettify : prettifyCode) as Observable<unknown>)
|
||||
.subscribe(() => this.codeFormatted.emit(), () => { /* ignore failure to format */ });
|
||||
}
|
||||
|
||||
/** Sets the message showing that the code could not be found. */
|
||||
|
|
Loading…
Reference in New Issue