-
Notifications
You must be signed in to change notification settings - Fork 141
Migrate Layout File to Typescript #2164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| const fs = require('fs-extra'); | ||
| const path = require('path'); | ||
| import * as fs from 'fs-extra'; | ||
| import * as path from 'path'; | ||
|
|
||
| const { Layout } = require('./Layout'); | ||
| import { Layout, LayoutConfig } from './Layout'; | ||
|
|
||
| const logger = require('../utils/logger'); | ||
| import * as logger from '../utils/logger'; | ||
|
|
||
| const FRONTMATTER_NONE_ATTR = 'none'; | ||
|
|
||
| class LayoutManager { | ||
| constructor(config) { | ||
| export class LayoutManager { | ||
| layouts: Record<string, Layout>; | ||
| layoutsRootPath: string; | ||
| config: LayoutConfig; | ||
|
|
||
| constructor(config: LayoutConfig) { | ||
| this.config = config; | ||
|
|
||
| this.layoutsRootPath = path.join(config.rootPath, '_markbind', 'layouts'); | ||
|
|
@@ -26,7 +30,7 @@ class LayoutManager { | |
| /** | ||
| * Update layouts which have the provided filePaths as dependencies | ||
| */ | ||
| updateLayouts(filePaths) { | ||
| updateLayouts(filePaths: string[]) { | ||
| const layoutsToRegenerate = Object.entries(this.layouts) | ||
| .filter(([, layout]) => layout.shouldRegenerate(filePaths)); | ||
|
|
||
|
|
@@ -36,7 +40,7 @@ class LayoutManager { | |
| })); | ||
| } | ||
|
|
||
| generateLayoutIfNeeded(name) { | ||
| generateLayoutIfNeeded(name: string) { | ||
| if (this.layouts[name]) { | ||
| return this.layouts[name].generatePromise; | ||
| } | ||
|
|
@@ -52,15 +56,15 @@ class LayoutManager { | |
| return this.layouts[name].generatePromise; | ||
| } | ||
|
|
||
| layoutHasPageNav(name) { | ||
| layoutHasPageNav(name: string) { | ||
| if (name === FRONTMATTER_NONE_ATTR) { | ||
| return false; | ||
| } | ||
|
|
||
| return this.layouts[name] && this.layouts[name].layoutPageNavUuid; | ||
| } | ||
|
|
||
| combineLayoutWithPage(name, pageContent, pageNav, pageIncludedFiles) { | ||
| combineLayoutWithPage(name: string, pageContent: string, pageNav: string, pageIncludedFiles: Set<string>) { | ||
| if (name === FRONTMATTER_NONE_ATTR) { | ||
| return pageContent; | ||
| } | ||
|
|
@@ -69,18 +73,14 @@ class LayoutManager { | |
| return pageContent; | ||
| } | ||
|
|
||
| return this.layouts[name].insertPage(pageContent, pageNav, pageIncludedFiles); | ||
| return this.layouts[name].insertPage(pageContent, pageNav, new Set<string>(pageIncludedFiles)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we create a new instance of |
||
| } | ||
|
|
||
| getLayoutPageNjkAssets(name) { | ||
| getLayoutPageNjkAssets(name: string) { | ||
| if (name === FRONTMATTER_NONE_ATTR || !this.layouts[name]) { | ||
| return {}; | ||
| } | ||
|
|
||
| return this.layouts[name].getPageNjkAssets(); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| LayoutManager, | ||
| }; | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export { Layout } from './Layout'; | ||
| export { LayoutManager } from './LayoutManager'; | ||
| export const LAYOUT_DEFAULT_NAME = 'default.md'; | ||
| export const LAYOUT_FOLDER_PATH = '_markbind/layouts'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,17 +18,14 @@ import type { SiteConfig } from '../Site/SiteConfig'; | |
| import type { FrontMatter } from '../plugins/Plugin'; | ||
| import type { ExternalManager } from '../External/ExternalManager'; | ||
| import { MbNode } from '../utils/node'; | ||
| import { LAYOUT_DEFAULT_NAME } from '../Layout/index'; | ||
|
|
||
| require('../patches/htmlparser2'); | ||
|
|
||
| const _ = { cloneDeep, isObject, isArray }; | ||
|
|
||
| const PACKAGE_VERSION = require('../../package.json').version; | ||
|
|
||
| const { | ||
| LAYOUT_DEFAULT_NAME, | ||
| } = require('../Layout'); | ||
|
|
||
| const TITLE_PREFIX_SEPARATOR = ' - '; | ||
| const TITLE_SUFFIX_SEPARATOR = ' - '; | ||
|
|
||
|
|
@@ -443,7 +440,8 @@ export class Page { | |
| */ | ||
| buildPageNav(content: string) { | ||
| const isFmPageNavSpecifierValid = this.isPageNavigationSpecifierValid(); | ||
| const doesLayoutHavePageNav = this.pageConfig.layoutManager.layoutHasPageNav(this.layout); | ||
| const doesLayoutHavePageNav = this.layout ? this.pageConfig.layoutManager.layoutHasPageNav( | ||
| this.layout) : false; | ||
|
Comment on lines
+443
to
+444
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can avoid all of these typing issues with Feel free to trace to verify that this is safe! |
||
|
|
||
| if (isFmPageNavSpecifierValid && doesLayoutHavePageNav) { | ||
| this.navigableHeadings = {}; | ||
|
|
@@ -502,13 +500,13 @@ export class Page { | |
| const pageContent = content; | ||
|
|
||
| pluginManager.collectPluginPageNjkAssets(this.frontmatter, content, this.asset); | ||
|
|
||
| await layoutManager.generateLayoutIfNeeded(this.layout); | ||
| await layoutManager.generateLayoutIfNeeded(this.layout ?? LAYOUT_DEFAULT_NAME); | ||
| const pageNav = this.buildPageNav(content); | ||
| content = layoutManager.combineLayoutWithPage(this.layout, content, pageNav, this.includedFiles); | ||
| content = layoutManager.combineLayoutWithPage(this.layout ?? LAYOUT_DEFAULT_NAME, | ||
| content, pageNav, this.includedFiles); | ||
| this.asset = { | ||
| ...this.asset, | ||
| ...layoutManager.getLayoutPageNjkAssets(this.layout), | ||
| ...layoutManager.getLayoutPageNjkAssets(this.layout ?? LAYOUT_DEFAULT_NAME), | ||
| }; | ||
|
|
||
| pageSources.addAllToSet(this.includedFiles); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.