LsxContext.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import * as url from 'url';
  2. import { customTagUtils, pathUtils } from 'growi-commons';
  3. const { TagContext, ArgsParser, OptionParser } = customTagUtils;
  4. export class LsxContext extends TagContext {
  5. /**
  6. * @param {object|TagContext|LsxContext} initArgs
  7. */
  8. constructor(initArgs) {
  9. super(initArgs);
  10. this.fromPagePath = null;
  11. // initialized after parse()
  12. this.isParsed = null;
  13. this.pagePath = null;
  14. this.options = {};
  15. }
  16. parse() {
  17. if (this.isParsed) {
  18. return;
  19. }
  20. const parsedResult = ArgsParser.parse(this.args);
  21. this.options = parsedResult.options;
  22. // determine specifiedPath
  23. // order:
  24. // 1: lsx(prefix=..., ...)
  25. // 2: lsx(firstArgs, ...)
  26. // 3: fromPagePath
  27. const specifiedPath = this.options.prefix
  28. || ((parsedResult.firstArgsValue === true) ? parsedResult.firstArgsKey : undefined)
  29. || this.fromPagePath;
  30. // resolve pagePath
  31. // when `fromPagePath`=/hoge and `specifiedPath`=./fuga,
  32. // `pagePath` to be /hoge/fuga
  33. // when `fromPagePath`=/hoge and `specifiedPath`=/fuga,
  34. // `pagePath` to be /fuga
  35. // when `fromPagePath`=/hoge and `specifiedPath`=undefined,
  36. // `pagePath` to be /hoge
  37. this.pagePath = (specifiedPath !== undefined)
  38. ? decodeURIComponent(url.resolve(pathUtils.addTrailingSlash(this.fromPagePath), specifiedPath))
  39. : this.fromPagePath;
  40. this.isParsed = true;
  41. }
  42. getOptDepth() {
  43. if (this.options.depth === undefined) {
  44. return undefined;
  45. }
  46. return OptionParser.parseRange(this.options.depth);
  47. }
  48. }