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(
|
expect(getFormattedCode()).toBe(
|
||||||
`Formatted code (language: auto, linenums: true): ${bigMultiLineCode}`);
|
`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', () => {
|
describe('whitespace handling', () => {
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { Clipboard } from '@angular/cdk/clipboard';
|
||||||
import { Logger } from 'app/shared/logger.service';
|
import { Logger } from 'app/shared/logger.service';
|
||||||
import { PrettyPrinter } from './pretty-printer.service';
|
import { PrettyPrinter } from './pretty-printer.service';
|
||||||
import { MatSnackBar } from '@angular/material/snack-bar';
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||||
|
import { Observable, of } from 'rxjs';
|
||||||
import { tap } from 'rxjs/operators';
|
import { tap } from 'rxjs/operators';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,15 +109,22 @@ export class CodeComponent implements OnChanges {
|
||||||
}
|
}
|
||||||
|
|
||||||
private formatDisplayedCode() {
|
private formatDisplayedCode() {
|
||||||
|
const linenums = this.getLinenums();
|
||||||
const leftAlignedCode = leftAlign(this.code);
|
const leftAlignedCode = leftAlign(this.code);
|
||||||
this.setCodeHtml(leftAlignedCode); // start with unformatted code
|
this.setCodeHtml(leftAlignedCode); // start with unformatted code
|
||||||
this.codeText = this.getCodeText(); // store the unformatted code as text (for copying)
|
this.codeText = this.getCodeText(); // store the unformatted code as text (for copying)
|
||||||
|
|
||||||
this.pretty
|
const skipPrettify = of(undefined);
|
||||||
.formatCode(leftAlignedCode, this.language, this.getLinenums())
|
const prettifyCode = this.pretty
|
||||||
.pipe(tap(() => this.codeFormatted.emit()))
|
.formatCode(leftAlignedCode, this.language, linenums)
|
||||||
.subscribe(c => this.setCodeHtml(c), () => { /* ignore failure to format */ }
|
.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. */
|
/** Sets the message showing that the code could not be found. */
|
||||||
|
|
Loading…
Reference in New Issue