| 34 | } |
| 35 | |
| 36 | class Page { |
| 37 | static async init(opts) { |
| 38 | opts = await Page.read(opts) |
| 39 | if (!opts) return |
| 40 | return new Page(opts) |
| 41 | } |
| 42 | |
| 43 | static async read(opts) { |
| 44 | assert(opts.languageCode, 'languageCode is required') |
| 45 | assert(opts.relativePath, 'relativePath is required') |
| 46 | assert(opts.basePath, 'basePath is required') |
| 47 | |
| 48 | const relativePath = slash(opts.relativePath) |
| 49 | const fullPath = slash(path.join(opts.basePath, relativePath)) |
| 50 | |
| 51 | // Per https://nodejs.org/api/fs.html#fs_fs_exists_path_callback |
| 52 | // its better to read and handle errors than to check access/stats first |
| 53 | try { |
| 54 | const { data, content, errors: frontmatterErrors } = await readFileContents(fullPath) |
| 55 | |
| 56 | return { |
| 57 | ...opts, |
| 58 | relativePath, |
| 59 | fullPath, |
| 60 | ...data, |
| 61 | markdown: content, |
| 62 | frontmatterErrors, |
| 63 | } |
| 64 | } catch (err) { |
| 65 | if (err.code === 'ENOENT') return false |
| 66 | console.error(err) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | constructor(opts) { |
| 71 | if (opts.frontmatterErrors && opts.frontmatterErrors.length) { |
| 72 | throw new FrontmatterErrorsError( |
| 73 | `${opts.frontmatterErrors.length} frontmatter errors trying to load ${opts.fullPath}`, |
| 74 | opts.frontmatterErrors |
| 75 | ) |
| 76 | } |
| 77 | delete opts.frontmatterErrors |
| 78 | Object.assign(this, { ...opts }) |
| 79 | |
| 80 | // Store raw data so we can cache parsed versions |
| 81 | this.rawIntro = this.intro |
| 82 | this.rawTitle = this.title |
| 83 | this.rawShortTitle = this.shortTitle |
| 84 | this.rawProduct = this.product |
| 85 | this.rawPermissions = this.permissions |
| 86 | this.rawLearningTracks = this.learningTracks |
| 87 | this.rawIncludeGuides = this.includeGuides |
| 88 | this.rawIntroLinks = this.introLinks |
| 89 | |
| 90 | // Is this the Homepage or a Product, Category, Topic, or Article? |
| 91 | this.documentType = getDocumentType(this.relativePath) |
| 92 | |
| 93 | // Get array of versions that the page is available in for fast lookup |
nothing calls this directly
no outgoing calls
no test coverage detected