lsx-context.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { customTagUtils, ParseRangeResult } from '@growi/core';
  2. const { OptionParser } = customTagUtils;
  3. export class LsxContext {
  4. pagePath: string;
  5. options?: Record<string, string|undefined>;
  6. constructor(pagePath: string, options: Record<string, string|undefined>) {
  7. this.pagePath = pagePath;
  8. // remove undefined keys
  9. Object.keys(options).forEach(key => options[key] === undefined && delete options[key]);
  10. this.options = options;
  11. }
  12. getOptDepth(): ParseRangeResult | null {
  13. if (this.options?.depth == null) {
  14. return null;
  15. }
  16. return OptionParser.parseRange(this.options.depth);
  17. }
  18. getStringifiedAttributes(separator = ', '): string {
  19. const attributeStrs = [`prefix=${this.pagePath}`];
  20. if (this.options != null) {
  21. const optionEntries = Object.entries(this.options).sort();
  22. attributeStrs.push(
  23. ...optionEntries.map(([key, val]) => `${key}=${val || 'true'}`),
  24. );
  25. }
  26. return attributeStrs.join(separator);
  27. }
  28. /**
  29. * for printing errors
  30. * @returns
  31. */
  32. toString(): string {
  33. return `$lsx(${this.getStringifiedAttributes()})`;
  34. }
  35. }