CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-docusaurus--utils

Node utility functions for Docusaurus packages providing URL handling, file operations, Git integration, i18n, Markdown processing, content visibility, and build utilities.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

url-web.mddocs/

URL and Web Utilities

Comprehensive URL handling, normalization, and web-related utilities for building and managing URLs in static site generation. These utilities provide robust URL manipulation, parsing, and validation for web applications.

Types

/**
 * Represents a URL path with optional query and hash components
 */
type URLPath = {
  pathname: string;
  search?: string;
  hash?: string;
};

Capabilities

URL Normalization and Building

normalizeUrl

Joins URL segments into a reasonable URL, handling protocols, slashes, and special cases.

/**
 * Joins URL segments into a reasonable URL
 * @param rawUrls - Array of URL segments to join
 * @returns Normalized URL string
 * @throws TypeError if any segment is not a string
 */
function normalizeUrl(rawUrls: string[]): string;

Usage Examples:

import { normalizeUrl } from "@docusaurus/utils";

// Basic URL joining
const url1 = normalizeUrl(["https://example.com", "docs", "intro"]);
// Result: "https://example.com/docs/intro"

// Handle trailing/leading slashes
const url2 = normalizeUrl(["https://example.com/", "/docs/", "/intro/"]);
// Result: "https://example.com/docs/intro"

// Preserve protocols
const url3 = normalizeUrl(["mailto:user@example.com"]);
// Result: "mailto:user@example.com"

File to URL Path Conversion

fileToPath

Converts file path to URL path, handling index files and extensions appropriately.

/**
 * Converts file path to URL path (e.g., 'index.md' → '/', 'foo/bar.js' → '/foo/bar')
 * @param file - File path to convert
 * @returns URL path string
 */
function fileToPath(file: string): string;

Usage Examples:

import { fileToPath } from "@docusaurus/utils";

// Index files become root paths
const indexPath = fileToPath("index.md");
// Result: "/"

const nestedIndex = fileToPath("docs/index.md");
// Result: "/docs/"

// Regular files lose extensions
const docPath = fileToPath("docs/introduction.md");
// Result: "/docs/introduction"

const jsPath = fileToPath("src/components/Button.js");
// Result: "/src/components/Button"

URL Encoding and Validation

encodePath

Encodes path using encodeURIComponent for each segment, assumes no query parameters.

/**
 * Encodes path using encodeURIComponent for each segment
 * @param userPath - Path to encode
 * @returns Encoded path string
 */
function encodePath(userPath: string): string;

isValidPathname

Checks if string is a valid pathname (absolute, no special characters).

/**
 * Checks if string is a valid pathname
 * @param str - String to validate
 * @returns True if valid pathname
 */
function isValidPathname(str: string): boolean;

URL Parsing and Components

parseURLOrPath

Parses URL or path with optional base, throws descriptive error on failure.

/**
 * Parses URL or path with optional base
 * @param url - URL or path string to parse
 * @param base - Optional base URL
 * @returns Parsed URL object
 * @throws Error with descriptive message on failure
 */
function parseURLOrPath(url: string, base?: string | URL): URL;

toURLPath

Converts URL object to URLPath, handling search and hash parsing edge cases.

/**
 * Converts URL object to URLPath
 * @param url - URL object to convert
 * @returns URLPath with pathname, search, and hash
 */
function toURLPath(url: URL): URLPath;

parseURLPath

Parses URL path with optional base path, resolving relative pathnames.

/**
 * Parses URL path with optional base path
 * @param urlPath - URL path string to parse
 * @param fromPath - Optional base path for resolution
 * @returns Parsed URLPath object
 */
function parseURLPath(urlPath: string, fromPath?: string): URLPath;

parseLocalURLPath

Parses local URL paths without resolving pathnames. Returns null for non-local URLs.

/**
 * Parses local URL paths without resolving pathnames
 * @param urlPath - URL path to parse
 * @returns URLPath object or null for non-local URLs
 */
function parseLocalURLPath(urlPath: string): URLPath | null;

serializeURLPath

Converts URLPath back to string representation.

/**
 * Converts URLPath back to string representation
 * @param urlPath - URLPath object to serialize
 * @returns String representation of the URL path
 */
function serializeURLPath(urlPath: URLPath): string;

Pathname Resolution

resolvePathname

Resolves pathnames using standard URL semantics.

/**
 * Resolves pathnames using standard URL semantics
 * @param to - Target pathname
 * @param from - Base pathname for resolution
 * @returns Resolved pathname
 */
function resolvePathname(to: string, from?: string): string;

Git Repository URL Utilities

hasSSHProtocol

Checks if URL uses SSH protocol (ssh: or git@host:path format).

/**
 * Checks if URL uses SSH protocol
 * @param sourceRepoUrl - Repository URL to check
 * @returns True if SSH protocol is used
 */
function hasSSHProtocol(sourceRepoUrl: string): boolean;

buildSshUrl

Constructs SSH URL for GitHub repository.

/**
 * Constructs SSH URL for GitHub repository
 * @param githubHost - GitHub host (e.g., "github.com")
 * @param organizationName - GitHub organization or user name
 * @param projectName - Repository name
 * @param githubPort - Optional SSH port
 * @returns SSH URL string
 */
function buildSshUrl(
  githubHost: string,
  organizationName: string,
  projectName: string,
  githubPort?: string
): string;

buildHttpsUrl

Constructs HTTPS URL for GitHub repository with credentials.

/**
 * Constructs HTTPS URL for GitHub repository with credentials
 * @param gitCredentials - Git credentials string
 * @param githubHost - GitHub host
 * @param organizationName - Organization or user name
 * @param projectName - Repository name
 * @param githubPort - Optional HTTPS port
 * @returns HTTPS URL with credentials
 */
function buildHttpsUrl(
  gitCredentials: string,
  githubHost: string,
  organizationName: string,
  projectName: string,
  githubPort?: string
): string;

Edit URL Generation

getEditUrl

Computes edit URL for a file. Returns undefined if editUrl is undefined.

/**
 * Computes edit URL for a file
 * @param fileRelativePath - Relative path to the file
 * @param editUrl - Base edit URL template
 * @returns Edit URL string or undefined
 */
function getEditUrl(fileRelativePath: string, editUrl?: string): string | undefined;

Usage Examples:

import {
  encodePath,
  isValidPathname,
  parseURLPath,
  resolvePathname,
  hasSSHProtocol,
  getEditUrl,
} from "@docusaurus/utils";

// Encode path with special characters
const encoded = encodePath("/docs/guide with spaces/intro");
// Result: "/docs/guide%20with%20spaces/intro"

// Validate pathname
const isValid = isValidPathname("/docs/intro");
// Result: true

const isInvalid = isValidPathname("../../../etc/passwd");
// Result: false

// Parse URL paths
const parsed = parseURLPath("/docs/intro?version=1.0#installation");
// Result: { pathname: "/docs/intro", search: "?version=1.0", hash: "#installation" }

// Resolve relative paths
const resolved = resolvePathname("../guide/intro", "/docs/reference/");
// Result: "/docs/guide/intro"

// Check SSH protocol
const isSSH = hasSSHProtocol("git@github.com:facebook/docusaurus.git");
// Result: true

const isHTTPS = hasSSHProtocol("https://github.com/facebook/docusaurus.git");
// Result: false

// Generate edit URL
const editUrl = getEditUrl(
  "docs/intro.md",
  "https://github.com/facebook/docusaurus/edit/main/"
);
// Result: "https://github.com/facebook/docusaurus/edit/main/docs/intro.md"

Install with Tessl CLI

npx tessl i tessl/npm-docusaurus--utils

docs

build-dev.md

config-constants.md

content-management.md

core-utilities.md

data-handling.md

filesystem-paths.md

git.md

index.md

markdown.md

url-web.md

tile.json