Topic.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. const libxml = require('libxmljs');
  17. /**
  18. * Class representing a doc topic
  19. */
  20. class Topic {
  21. /**
  22. * Create a topic
  23. *
  24. * @param {string} docRootPath - The start path
  25. * @param {string} ref - The relative path of the topic
  26. */
  27. constructor(docRootPath, ref) {
  28. this.docRootPath = docRootPath;
  29. this.ref = ref;
  30. this.children = [];
  31. // These are set during parsing
  32. this.fragment = undefined;
  33. this.domXml = new libxml.Document().node('div');
  34. }
  35. toJson() {
  36. return JSON.stringify({
  37. body: this.domXml.toString(),
  38. title: this.fragment.title
  39. .text()
  40. .replace(/[\n\r]/g, ' ')
  41. .trim()
  42. });
  43. }
  44. }
  45. module.exports = Topic;