Skip to content

Commit

Permalink
Rename to MbNode
Browse files Browse the repository at this point in the history
  • Loading branch information
jovyntls committed Mar 14, 2023
1 parent 51944ce commit dd78e39
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
20 changes: 10 additions & 10 deletions packages/core/src/html/NodeProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { PageNavProcessor, renderSiteNav, addSitePageNavPortal } from './siteAnd
import { highlightCodeBlock, setCodeLineNumbers } from './codeblockProcessor';
import { setHeadingId, assignPanelId } from './headerProcessor';
import { FootnoteProcessor } from './FootnoteProcessor';
import { Node, NodeOrText, TextElement } from '../utils/node';
import { MbNode, NodeOrText, TextElement } from '../utils/node';

const fm = require('fastmatter');

Expand Down Expand Up @@ -90,7 +90,7 @@ export class NodeProcessor {

static _trimNodes(nodeOrText: NodeOrText) {
if (NodeProcessor._isText(nodeOrText)) return;
const node = nodeOrText as Node;
const node = nodeOrText as MbNode;
if (node.name === 'pre' || node.name === 'code') {
return;
}
Expand All @@ -115,7 +115,7 @@ export class NodeProcessor {
/*
* Frontmatter collection
*/
_processFrontmatter(node: Node, context: Context) {
_processFrontmatter(node: MbNode, context: Context) {
let currentFrontmatter = {};
const frontmatter = cheerio(node);
if (!context.processingOptions.omitFrontmatter && frontmatter.text().trim()) {
Expand All @@ -124,8 +124,8 @@ export class NodeProcessor {
// The latter case will result in the data being wrapped in a div
const frontmatterIncludeDiv = frontmatter.find('div');
const frontmatterData = frontmatterIncludeDiv.length
? ((frontmatterIncludeDiv[0] as Node).children as Node[])[0].data
: ((frontmatter[0] as Node).children as Node[])[0].data;
? ((frontmatterIncludeDiv[0] as MbNode).children as MbNode[])[0].data
: ((frontmatter[0] as MbNode).children as MbNode[])[0].data;
const frontmatterWrapped = `${FRONTMATTER_FENCE}\n${frontmatterData}\n${FRONTMATTER_FENCE}`;

currentFrontmatter = fm(frontmatterWrapped).attributes;
Expand All @@ -142,7 +142,7 @@ export class NodeProcessor {
* Layout element collection
*/

private static collectLayoutEl(node: Node): string | null {
private static collectLayoutEl(node: MbNode): string | null {
const $ = cheerio(node);
const html = $.html();
$.remove();
Expand All @@ -152,7 +152,7 @@ export class NodeProcessor {
/**
* Removes the node if modal id already exists, processes node otherwise
*/
private processModal(node: Node) {
private processModal(node: MbNode) {
if (node.attribs) {
if (this.processedModals[node.attribs.id]) {
cheerio(node).remove();
Expand All @@ -174,7 +174,7 @@ export class NodeProcessor {
processNode(nodeOrText: NodeOrText, context: Context): Context {
try {
if (NodeProcessor._isText(nodeOrText)) return context;
const node = nodeOrText as Node;
const node = nodeOrText as MbNode;

transformOldSlotSyntax(node);
shiftSlotNodeDeeper(node);
Expand Down Expand Up @@ -275,7 +275,7 @@ export class NodeProcessor {

postProcessNode(nodeOrText: NodeOrText) {
if (NodeProcessor._isText(nodeOrText)) return;
const node = nodeOrText as Node;
const node = nodeOrText as MbNode;

try {
switch (node.name) {
Expand Down Expand Up @@ -322,7 +322,7 @@ export class NodeProcessor {
if (NodeProcessor._isText(dom)) {
return dom as TextElement;
}
const node = dom as Node;
const node = dom as MbNode;
node.name = node.name.toLowerCase();
if (linkProcessor.hasTagLink(node)) {
linkProcessor.convertRelativeLinks(node, context.cwf, this.config.rootPath, this.config.baseUrl);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/html/elements.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import cheerio from 'cheerio';
import pick from 'lodash/pick';
import { Node, NodeOrText } from '../utils/node';
import { MbNode, NodeOrText } from '../utils/node';

const _ = { pick };

export function createErrorNode(element: NodeOrText, error: any) {
const errorElement = cheerio.parseHTML(
`<div style="color: red">${error.message}</div>`, undefined, true,
)[0];
return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children'])) as Node;
return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children'])) as MbNode;
}

export function createEmptyNode() {
return cheerio.parseHTML('<div></div>', undefined, true)[0];
}

export function createSlotTemplateNode(slotName: string, content: string): Node[] {
export function createSlotTemplateNode(slotName: string, content: string): MbNode[] {
return cheerio.parseHTML(
`<template #${slotName}>${content}</template>`, undefined, true,
) as unknown as Node[];
) as unknown as MbNode[];
}
16 changes: 8 additions & 8 deletions packages/core/src/html/includePanelProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as urlUtil from '../utils/urlUtil';
import type { Context } from './Context';
import type { PageSources } from '../Page/PageSources';
import type VariableProcessor from '../variables/VariableProcessor';
import { Node, NodeOrText } from '../utils/node';
import { MbNode, NodeOrText } from '../utils/node';

require('../patches/htmlparser2');

Expand All @@ -26,7 +26,7 @@ const _ = { has, isEmpty };
/**
* Returns a boolean representing whether the file specified exists.
*/
function _checkAndWarnFileExists(element: Node, context: Context, actualFilePath: string,
function _checkAndWarnFileExists(element: MbNode, context: Context, actualFilePath: string,
pageSources: PageSources, isOptional = false) {
if (!fsUtil.fileExists(actualFilePath)) {
if (isOptional) {
Expand All @@ -48,7 +48,7 @@ function _checkAndWarnFileExists(element: Node, context: Context, actualFilePath
return true;
}

function _getBoilerplateFilePath(element: Node, filePath: string, config: Record<string, any>) {
function _getBoilerplateFilePath(element: MbNode, filePath: string, config: Record<string, any>) {
const isBoilerplate = _.has(element.attribs, 'boilerplate');
if (isBoilerplate) {
element.attribs.boilerplate = element.attribs.boilerplate || path.basename(filePath);
Expand All @@ -62,7 +62,7 @@ function _getBoilerplateFilePath(element: Node, filePath: string, config: Record
/**
* Retrieves several flags and file paths from the src attribute specified in the element.
*/
function _getSrcFlagsAndFilePaths(element: Node, config: Record<string, any>) {
function _getSrcFlagsAndFilePaths(element: MbNode, config: Record<string, any>) {
const isUrl = urlUtil.isUrl(element.attribs.src);

// We do this even if the src is not a url to get the hash, if any
Expand Down Expand Up @@ -104,7 +104,7 @@ function _getSrcFlagsAndFilePaths(element: Node, config: Record<string, any>) {
* Otherwise, sets the fragment attribute of the panel as parsed from the src,
* and adds the appropriate include.
*/
export function processPanelSrc(node: Node, context: Context, pageSources: PageSources,
export function processPanelSrc(node: MbNode, context: Context, pageSources: PageSources,
config: Record<string, any>): Context {
const hasSrc = _.has(node.attribs, 'src');
if (!hasSrc) {
Expand Down Expand Up @@ -148,7 +148,7 @@ export function processPanelSrc(node: Node, context: Context, pageSources: PageS
* Includes
*/

function _deleteIncludeAttributes(node: Node) {
function _deleteIncludeAttributes(node: MbNode) {
// Delete variable attributes in include tags as they are no longer needed
// e.g. '<include var-title="..." var-xx="..." />'
Object.keys(node.attribs).forEach((attribute) => {
Expand All @@ -170,7 +170,7 @@ function _deleteIncludeAttributes(node: Node) {
* Replaces it with an error node if the specified src is invalid,
* or an empty node if the src is invalid but optional.
*/
export function processInclude(node: Node, context: Context, pageSources: PageSources,
export function processInclude(node: MbNode, context: Context, pageSources: PageSources,
variableProcessor: VariableProcessor, renderMd: (text: string) => string,
renderMdInline: (text: string) => string,
config: Record<string, any>): Context {
Expand Down Expand Up @@ -270,7 +270,7 @@ export function processInclude(node: Node, context: Context, pageSources: PageSo
* Replaces it with an error node if the specified src is invalid.
* Else, appends the content to the node.
*/
export function processPopoverSrc(node: Node, context: Context, pageSources: PageSources,
export function processPopoverSrc(node: MbNode, context: Context, pageSources: PageSources,
variableProcessor: VariableProcessor, renderMd: (text: string) => string,
config: Record<string, any>): Context {
if (!_.has(node.attribs, 'src')) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { DomElement } from 'htmlparser2';

export type TextElement = DomElement;

export type Node = DomElement & cheerio.Element & {
export type MbNode = DomElement & cheerio.Element & {
name: string,
attribs: { [key: string]: any },
children: NodeOrText[],
};

export type NodeOrText = TextElement | Node;
export type NodeOrText = TextElement | MbNode;

0 comments on commit dd78e39

Please sign in to comment.