Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

codehike

Package Overview
Dependencies
Maintainers
0
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

codehike - npm Package Compare versions

Comparing version 1.0.0-beta.2 to 1.0.0-beta.3

dist/code/block.d.ts

2

dist/blocks.d.ts

@@ -6,3 +6,5 @@ /// <reference types="react" />

export declare function getBlocks(Content: MDXContent, props?: MDXProps): unknown;
export declare function parseRoot<Output, Def extends ZodTypeDef, Input>(Content: MDXContent, Schema?: undefined, props?: MDXProps): unknown;
export declare function parseRoot<Output, Def extends ZodTypeDef, Input>(Content: MDXContent, Schema: z.ZodType<Output, Def, Input>, props?: MDXProps): Output;
export declare const parse: typeof parseRoot;
export declare const Block: z.ZodObject<{

@@ -9,0 +11,0 @@ title: z.ZodOptional<z.ZodString>;

8

dist/blocks.js

@@ -10,5 +10,9 @@ import { z } from "zod";

export function parseRoot(Content, Schema, props = {}) {
const data = getBlocks(Content, props);
return parseProps(data, Schema);
const data = getBlocks(Content, props || {});
if (Schema) {
return parseProps(data, Schema);
}
return data;
}
export const parse = parseRoot;
export const Block = z.object({

@@ -15,0 +19,0 @@ title: z.string().optional(),

@@ -11,3 +11,3 @@ import { jsx as _jsx } from "react/jsx-runtime";

const { _ref, data, ...props } = result;
return _jsx("pre", { ...props, ref: _ref });
return _jsx("pre", { ...props, ref: _ref, "data-ch": true });
}

@@ -30,3 +30,3 @@ };

else {
const { lineNumber, totalLines, indentation, data, ...props } = result;
const { lineNumber, totalLines, indentation, data, annotation, ...props } = result;
return _jsx("div", { ...props });

@@ -43,5 +43,5 @@ }

else {
const { value, lineNumber, data, ...props } = result;
const { value, data, ...props } = result;
return _jsx("span", { ...props, children: value });
}
};
import { jsx as _jsx } from "react/jsx-runtime";
import { forwardRef } from "react";
import { RenderLineContent, toLineContent } from "./tokens.js";
import { isBlockAnnotation, isInlineAnnotation, } from "./types.js";
import { AddRefIfNedded } from "./pre-ref.js";
import { InnerLine, InnerPre } from "./inner.js";
export const Pre = forwardRef(({ code, handlers = [], className, ...rest }, ref) => {
import { renderLines } from "./block.js";
import { toLineGroups, toLines } from "./lines.js";
import { InnerPre } from "./inner.js";
export const Pre = forwardRef(({ code, handlers = [], ...rest }, ref) => {
let { tokens, themeName, lang, annotations } = code;
if (!tokens) {
throw new Error("Missing tokens in code block. Use the `highlight` function to generate the tokens.");
}
handlers

@@ -14,168 +18,26 @@ .filter((c) => c.transform)

});
if (!tokens) {
throw new Error("Missing tokens in code block. Use the `highlight` function to generate the tokens.");
}
const annotationNames = new Set(annotations.map((a) => a.name));
const hs = handlers.filter((h) => !h.onlyIfAnnotated || annotationNames.has(h.name));
const stack = buildPreStack(hs);
const merge = { _stack: stack, _ref: ref };
return (_jsx(InnerPre, { merge: merge, "data-theme": themeName, "data-lang": lang, ...rest, children: _jsx(PreContent, { tokens: tokens, handlers: hs, annotations: annotations }) }));
});
function PreContent({ tokens, handlers, annotations, }) {
const lines = toLines(tokens);
const indentations = lines.map((line) => { var _a, _b; return ((_b = (_a = line.tokens[0]) === null || _a === void 0 ? void 0 : _a.value.match(/^\s*/)) === null || _b === void 0 ? void 0 : _b[0].length) || 0; });
const blockAnnotations = annotations.filter(isBlockAnnotation);
const inlineAnnotations = annotations.filter(isInlineAnnotation);
const annotationNames = new Set(annotations.map((a) => a.name));
const groups = toLineGroups(lines, blockAnnotations);
const noRefStack = handlers
.filter(({ Pre, name, onlyIfAnnotated }) => Pre && (!onlyIfAnnotated || annotationNames.has(name)))
.map(({ Pre }) => Pre);
const refStack = handlers
.filter(({ PreWithRef, name, onlyIfAnnotated }) => PreWithRef && (!onlyIfAnnotated || annotationNames.has(name)))
.map(({ PreWithRef }) => PreWithRef);
return renderLines({
linesOrGroups: groups,
handlers,
inlineAnnotations,
});
}
function buildPreStack(handlers) {
const noRefStack = handlers.map(({ Pre }) => Pre).filter(Boolean);
const refStack = handlers.map(({ PreWithRef }) => PreWithRef).filter(Boolean);
if (refStack.length > 0) {
refStack.unshift(AddRefIfNedded);
}
const stack = [...noRefStack, ...refStack];
const merge = { _stack: stack, _ref: ref };
return (_jsx(InnerPre, { merge: merge, "data-theme": themeName, "data-lang": lang, className: className, ...rest, children: _jsx(RenderLines, { linesOrGroups: groups, annotationNames: annotationNames, handlers: handlers, inlineAnnotations: inlineAnnotations, indentations: indentations, totalLines: lines.length }) }));
});
function RenderLines({ linesOrGroups, handlers, inlineAnnotations, indentations, annotationStack = [], annotationNames, totalLines, }) {
return linesOrGroups.map((group) => {
if (isGroup(group)) {
return (_jsx(AnnotatedLines, { group: group, handlers: handlers, inlineAnnotations: inlineAnnotations, annotationStack: annotationStack, indentations: indentations, annotationNames: annotationNames, totalLines: totalLines }, group.range[0]));
}
const lineNumber = group.range[0];
const indentation = indentations[lineNumber - 1];
const lineAnnotations = inlineAnnotations.filter((annotation) => annotation.lineNumber === lineNumber);
const lineContent = toLineContent(group.tokens, lineAnnotations);
const stack = handlers.flatMap(({ name, Line, AnnotatedLine, onlyIfAnnotated }) => {
if (onlyIfAnnotated && !annotationNames.has(name)) {
return [];
}
const s = [];
const annotation = annotationStack.find((a) => a.name === name);
if (annotation && AnnotatedLine) {
s.push({ Component: AnnotatedLine, annotation });
}
if (Line) {
s.push({ Component: Line, annotation });
}
return s;
});
let children = (_jsx(RenderLineContent, { lineContent: lineContent, handlers: handlers, lineNumber: lineNumber }));
const merge = { lineNumber, indentation, totalLines, _stack: stack };
return (_jsx(InnerLine, { merge: merge, children: children }, lineNumber));
});
return [...noRefStack, ...refStack];
}
function AnnotatedLines({ group, handlers, inlineAnnotations, annotationStack, indentations, annotationNames, totalLines, }) {
var _a;
const { annotation, lines } = group;
const { name } = annotation;
const Component = (_a = handlers.find((c) => c.name === name)) === null || _a === void 0 ? void 0 : _a.Block;
if (!Component) {
return (_jsx(RenderLines, { linesOrGroups: lines, handlers: handlers, inlineAnnotations: inlineAnnotations, annotationStack: [annotation, ...annotationStack], indentations: indentations, annotationNames: annotationNames, totalLines: totalLines }));
}
return (_jsx(Component, { annotation: annotation, children: _jsx(RenderLines, { linesOrGroups: lines, handlers: handlers, inlineAnnotations: inlineAnnotations, annotationStack: [annotation, ...annotationStack], indentations: indentations, annotationNames: annotationNames, totalLines: totalLines }) }));
}
function toLines(tokens) {
const lines = [[]];
const tokenStack = tokens.slice();
let col = 1;
while (tokenStack.length) {
const token = tokenStack.shift();
if (typeof token === "string") {
const [value, ...tail] = token.split("\n");
if (value) {
let start = col;
col += value.length;
lines[lines.length - 1].push({
value,
range: [start, col],
});
}
if (tail.length) {
lines[lines.length - 1].push({
value: "\n",
range: [col, col + 1],
});
lines.push([]);
col = 1;
tokenStack.unshift(tail.join("\n"));
}
}
else {
const [value, color, rest = {}] = token;
let start = col;
col += value.length;
lines[lines.length - 1].push({
value,
style: { color, ...rest },
range: [start, col],
});
}
}
return lines.map((tokens, i) => ({ tokens, range: [i + 1, i + 1] }));
}
function toLineGroups(lines, annotations) {
let groups = lines;
for (let i = annotations.length - 1; i >= 0; i--) {
const annotation = annotations[i];
groups = applyBlockAnnotation(groups, annotation);
}
return groups;
}
function applyBlockAnnotation(lines, annotation) {
const { fromLineNumber, toLineNumber } = annotation;
const range = [fromLineNumber, toLineNumber];
let groups = splitGroups(lines, range[0]);
groups = splitGroups(groups, range[1] + 1);
let before = [];
let inside = [];
let after = [];
groups.forEach((group) => {
if (group.range[1] < range[0]) {
before.push(group);
}
else if (group.range[0] > range[1]) {
after.push(group);
}
else {
inside.push(group);
}
});
return [
...before,
{
annotation,
lines: inside,
range: [inside[0].range[0], inside[inside.length - 1].range[1]],
},
...after,
];
}
function splitGroups(groups, lineNumber) {
const index = groups.findIndex((group) => {
return (isGroup(group) &&
group.range[0] < lineNumber &&
lineNumber <= group.range[1]);
});
if (index === -1) {
return groups;
}
const group = groups[index];
const lines = splitGroups(group.lines, lineNumber);
let before = [];
let after = [];
lines.forEach((lineOrGroup) => {
if (lineOrGroup.range[1] < lineNumber) {
before.push(lineOrGroup);
}
else {
after.push(lineOrGroup);
}
});
return [
...groups.slice(0, index),
{ ...group, lines: before, range: [group.range[0], lineNumber - 1] },
{ ...group, lines: after, range: [lineNumber, group.range[1]] },
...groups.slice(index + 1),
];
}
function isGroup(item) {
return item.annotation !== undefined;
}

@@ -1,3 +0,3 @@

import { AnnotationHandler, InlineAnnotation, InternalToken } from "./types.js";
type TokenGroup = {
import { InlineAnnotation, InternalToken } from "./types.js";
export type TokenGroup = {
annotation: InlineAnnotation;

@@ -7,10 +7,5 @@ content: LineContent;

};
type LineContent = (InternalToken | TokenGroup)[];
export declare function RenderLineContent({ lineContent, handlers, lineNumber, }: {
lineContent: LineContent;
handlers: AnnotationHandler[];
lineNumber: number;
}): (string | import("react/jsx-runtime").JSX.Element)[];
export type LineContent = (InternalToken | TokenGroup)[];
export declare function toLineContent(tokens: InternalToken[], annotations: InlineAnnotation[]): LineContent;
export {};
export declare function isGroup(token: InternalToken | TokenGroup): token is TokenGroup;
//# sourceMappingURL=tokens.d.ts.map

@@ -1,43 +0,1 @@

import { jsx as _jsx } from "react/jsx-runtime";
import { InnerToken } from "./inner.js";
export function RenderLineContent({ lineContent, handlers, lineNumber, }) {
// TODO get Token from annotationStack
const annotationStack = [];
const stack = handlers.flatMap(({ name, Token, AnnotatedToken }) => {
const s = [];
const annotation = annotationStack.find((a) => a.name === name);
if (annotation && AnnotatedToken) {
s.push({ Component: AnnotatedToken, annotation });
}
if (Token) {
s.push({ Component: Token });
}
return s;
});
return lineContent.map((item, i) => {
if (isGroup(item)) {
return (_jsx(AnnotatedTokens, { group: item, handlers: handlers, lineNumber: lineNumber }, i));
}
else {
return item.style ? (_jsx(InnerToken, { merge: {
_stack: stack,
style: item.style,
value: item.value,
lineNumber,
} }, i)) : (
// whitespace
item.value);
}
});
}
function AnnotatedTokens({ lineNumber, group, handlers, }) {
var _a;
const { annotation, content } = group;
const { name } = annotation;
const Component = (_a = handlers.find((c) => c.name === name)) === null || _a === void 0 ? void 0 : _a.Inline;
if (!Component) {
return (_jsx(RenderLineContent, { lineContent: content, handlers: handlers, lineNumber: lineNumber }));
}
return (_jsx(Component, { annotation: annotation, lineNumber: lineNumber, children: _jsx(RenderLineContent, { lineContent: content, handlers: handlers, lineNumber: lineNumber }) }));
}
export function toLineContent(tokens, annotations) {

@@ -130,4 +88,4 @@ let content = tokens;

}
function isGroup(token) {
export function isGroup(token) {
return token.content !== undefined;
}

@@ -75,3 +75,2 @@ /// <reference types="react" />

annotation: InlineAnnotation;
lineNumber: number;
children: React.ReactNode;

@@ -107,2 +106,3 @@ }>;

indentation: number;
annotation?: BlockAnnotation;
data?: Record<string, any>;

@@ -114,13 +114,11 @@ _stack: {

};
export type CustomLine = React.ComponentType<CustomLineProps & {
annotation?: BlockAnnotation;
}>;
export type CustomLine = React.ComponentType<CustomLineProps>;
export type CustomLineWithAnnotation = React.ComponentType<CustomLineProps & {
annotation: BlockAnnotation;
}>;
export type CustomTokenProps = {
export type CustomTokenProps = React.ComponentProps<"span"> & {
value: string;
lineNumber: number;
style?: React.CSSProperties;
data?: Record<string, any>;
annotation?: BlockAnnotation | InlineAnnotation;
_stack: {

@@ -127,0 +125,0 @@ Component: CustomToken | CustomTokenWithAnnotation;

import React from "react";
export declare function Scroller({ onIndexChange, triggerPosition, children, }: {
export declare function Scroller({ onIndexChange, triggerPosition, rootMargin, children, }: {
onIndexChange: (index: number) => void;
triggerPosition?: TriggerPosition;
rootMargin?: string;
children: React.ReactNode;

@@ -6,0 +7,0 @@ }): import("react/jsx-runtime").JSX.Element;

@@ -5,3 +5,3 @@ import { jsx as _jsx } from "react/jsx-runtime";

const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
export function Scroller({ onIndexChange, triggerPosition = "50%", children, }) {
export function Scroller({ onIndexChange, triggerPosition = "50%", rootMargin, children, }) {
const [observer, setObserver] = React.useState();

@@ -20,3 +20,3 @@ const vh = useWindowHeight();

};
const observer = newIntersectionObserver(handleIntersect, defaultRootMargin(windowHeight, triggerPosition));
const observer = newIntersectionObserver(handleIntersect, rootMargin || defaultRootMargin(windowHeight, triggerPosition));
setObserver(observer);

@@ -23,0 +23,0 @@ return () => observer.disconnect();

import React from "react";
export declare function SelectionProvider({ children, ...rest }: React.HTMLAttributes<HTMLDivElement>): import("react/jsx-runtime").JSX.Element;
export declare function SelectionProvider({ children, rootMargin, ...rest }: React.HTMLAttributes<HTMLDivElement> & {
rootMargin?: string;
}): import("react/jsx-runtime").JSX.Element;
export declare function Selectable({ index, selectOn, ...rest }: {

@@ -4,0 +6,0 @@ index: number;

@@ -9,5 +9,5 @@ "use client";

});
export function SelectionProvider({ children, ...rest }) {
export function SelectionProvider({ children, rootMargin, ...rest }) {
const [selectedIndex, selectIndex] = React.useState(0);
return (_jsx("div", { "data-selected-index": selectedIndex, ...rest, children: _jsx(Scroller, { onIndexChange: selectIndex, children: _jsx(StepsContext.Provider, { value: {
return (_jsx("div", { "data-selected-index": selectedIndex, ...rest, children: _jsx(Scroller, { onIndexChange: selectIndex, rootMargin: rootMargin, children: _jsx(StepsContext.Provider, { value: {
selectedIndex,

@@ -14,0 +14,0 @@ selectIndex,

{
"name": "codehike",
"version": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"type": "module",

@@ -30,2 +30,3 @@ "sideEffects": false,

"estree-util-visit": "^2.0.0",
"front-matter": "^4.0.2",
"mdast-util-mdx-jsx": "^3.0.0",

@@ -42,2 +43,3 @@ "unist-util-visit": "^5.0.0"

"@types/react-dom": "^18.2.18",
"@vitest/ui": "^2.0.3",
"mdast-util-from-markdown": "^2.0.0",

@@ -48,3 +50,3 @@ "react": "18.3.0-canary-c5b937576-20231219",

"unified": "^11.0.4",
"vitest": "^1.6.0",
"vitest": "^2.0.3",
"zod": "^3.22.4"

@@ -51,0 +53,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc