CodeSampleX

示例

marked 18.0.5: Marked

已验证示例 — npm marked 18.0.5: Marked. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: Marked is a constructor function exported by marked

sha256:cda01d5f6a57be583c41d8d922fdb37634e0cb9f3223d0ebec1306597189db91

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-28

案例

HOW
目标
verify marked.Marked in pkg:npm/marked@18.0.5
包
符号
  • marked.Marked
创建时间
2026-08-28T00:02:10Z

契约

  1. Marked is a constructor function exported by marked
  2. new Marked() creates an isolated markdown parser instance
  3. Marked instance parse method parses markdown into HTML synchronously by default
  4. Marked instance parseInline method parses inline markdown without wrapping in paragraph tags
  5. Marked instance lexer method tokenizes markdown into structured token objects
  6. Marked instance parser method renders token objects into an HTML string
  7. Marked instance walkTokens method traverses token tree nodes
  8. Marked instance use method applies extensions without mutating global marked state
  9. Marked instance setOptions method modifies instance configuration

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.mjs

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify marked.Marked in pkg:npm/marked@18.0.5
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/marked@18.0.5
Demonstrate these symbols/APIs:
  - marked.Marked

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:1ab58d2435faea0cfb0c568ca72dc7a1c43826b1c1e9bc0edb0423b3eace1374","contract":["Marked is a constructor function exported by marked","new Marked() creates an isolated markdown parser instance","Marked instance parse method parses markdown into HTML synchronously by default","Marked instance parseInline method parses inline markdown without wrapping in paragraph tags","Marked instance lexer method tokenizes markdown into structured token objects","Marked instance parser method renders token objects into an HTML string","Marked instance walkTokens method traverses token tree nodes","Marked instance use method applies extensions without mutating global marked state","Marked instance setOptions method modifies instance configuration"],"goal":"verify marked.Marked in pkg:npm/marked@18.0.5","kind":"HOW","packages":["pkg:npm/marked@18.0.5"],"schemaVersion":1,"symbols":["marked.Marked"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/marked@18.0.5"],"schemaVersion":1,"subject":"pkg:npm/marked@18.0.5","symbols":["marked.Marked"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-marked-marked-class",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-marked-marked-class",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "marked": "18.0.5"
      }
    },
    "node_modules/marked": {
      "version": "18.0.5",
      "resolved": "https://registry.npmjs.org/marked/-/marked-18.0.5.tgz",
      "integrity": "sha512-S6GcvALHg6K4ohtu4E7x0a1AqhAjp6cV8KhLSyN9qVapnzJkusVBxZRcIU9AeYsbe6P1hKDusSbEOzGyyuce6w==",
      "license": "MIT",
      "bin": {
        "marked": "bin/marked.js"
      },
      "engines": {
        "node": ">= 20"
      }
    }
  }
}
package.json
{
  "name": "sample-marked-marked-class",
  "version": "1.0.0",
  "private": true,
  "license": "MIT-0",
  "main": "src/index.js",
  "dependencies": {
    "marked": "18.0.5"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify marked.Marked in pkg:npm/marked@18.0.5",
  "kind": "HOW",
  "packages": [
    "pkg:npm/marked@18.0.5"
  ],
  "symbols": [
    "marked.Marked"
  ]
}
src/index.js
const { Marked } = require('marked');

/**
 * Creates an isolated Marked instance with optional configuration and extensions.
 *
 * @param {...object} extensions - Optional extension objects or Marked options.
 * @returns {Marked} A new Marked instance.
 */
function createMarked(...extensions) {
  return new Marked(...extensions);
}

/**
 * Parses markdown using a dedicated Marked instance.
 *
 * @param {string} src - Markdown string to parse.
 * @param {Marked} [instance] - Optional Marked instance to use.
 * @returns {string|Promise<string>} Rendered HTML string.
 */
function parseWithInstance(src, instance = new Marked()) {
  return instance.parse(src);
}

/**
 * Parses inline markdown tokens using a dedicated Marked instance.
 *
 * @param {string} src - Inline markdown string.
 * @param {Marked} [instance] - Optional Marked instance to use.
 * @returns {string} Rendered inline HTML string.
 */
function parseInlineWithInstance(src, instance = new Marked()) {
  return instance.parseInline(src);
}

/**
 * Tokenizes markdown source into structured token nodes using a Marked instance.
 *
 * @param {string} src - Markdown input string.
 * @param {Marked} [instance] - Optional Marked instance to use.
 * @returns {Array<object>} Array of parsed token objects.
 */
function tokenizeWithInstance(src, instance = new Marked()) {
  return instance.lexer(src);
}

/**
 * Compiles a token list into HTML output using a Marked instance.
 *
 * @param {Array<object>} tokens - Array of token objects.
 * @param {Marked} [instance] - Optional Marked instance to use.
 * @returns {string} Compiled HTML string.
 */
function compileTokensWithInstance(tokens, instance = new Marked()) {
  return instance.parser(tokens);
}

module.exports = {
  Marked,
  createMarked,
  parseWithInstance,
  parseInlineWithInstance,
  tokenizeWithInstance,
  compileTokensWithInstance
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  Marked,
  createMarked,
  parseWithInstance,
  parseInlineWithInstance,
  tokenizeWithInstance,
  compileTokensWithInstance
} = require('../src/index.js');
const markedModule = require('marked');

// 1. Marked is a constructor function exported by marked
assert.equal(typeof Marked, 'function', 'Marked must be a constructor function');
assert.strictEqual(Marked, markedModule.Marked, 'Marked export must match marked.Marked');

// 2. new Marked() creates an isolated markdown parser instance
const instance = new Marked();
assert.ok(instance instanceof Marked, 'instance must be an instance of Marked');
assert.equal(typeof instance.parse, 'function', 'instance.parse must be a function');
assert.equal(typeof instance.parseInline, 'function', 'instance.parseInline must be a function');
assert.equal(typeof instance.lexer, 'function', 'instance.lexer must be a function');
assert.equal(typeof instance.parser, 'function', 'instance.parser must be a function');
assert.equal(typeof instance.use, 'function', 'instance.use must be a function');
assert.equal(typeof instance.setOptions, 'function', 'instance.setOptions must be a function');
assert.equal(typeof instance.walkTokens, 'function', 'instance.walkTokens must be a function');

const helperInstance = createMarked();
assert.ok(helperInstance instanceof Marked, 'createMarked helper must return Marked instance');

// 3. Marked instance parse method parses markdown into HTML synchronously by default
const markdownInput = '# Heading Title\n\nThis is **bold** text and *italic* text.';
const renderedHtml = instance.parse(markdownInput);
assert.equal(typeof renderedHtml, 'string', 'parse output must be a string');
assert.ok(renderedHtml.includes('<h1>Heading Title</h1>'), 'rendered HTML must contain <h1>Heading Title</h1>');
assert.ok(
  renderedHtml.includes('<p>This is <strong>bold</strong> text and <em>italic</em> text.</p>'),
  'rendered HTML must contain formatted paragraph'
);

const helperHtml = parseWithInstance(markdownInput, instance);
assert.equal(helperHtml, renderedHtml, 'parseWithInstance helper must produce identical HTML');

// 4. Marked instance parseInline method parses inline markdown without wrapping in paragraph tags
const inlineInput = '**Bold text** and [Link](https://example.com)';
const inlineHtml = instance.parseInline(inlineInput);
assert.equal(typeof inlineHtml, 'string', 'parseInline output must be a string');
assert.ok(inlineHtml.includes('<strong>Bold text</strong>'), 'parseInline must render bold tags');
assert.ok(inlineHtml.includes('<a href="https://example.com">Link</a>'), 'parseInline must render anchor tags');
assert.ok(!inlineHtml.includes('<p>'), 'parseInline must not wrap content in paragraph tags');

const helperInline = parseInlineWithInstance(inlineInput, instance);
assert.equal(helperInline, inlineHtml, 'parseInlineWithInstance helper must match parseInline output');

// 5. Marked instance lexer method tokenizes markdown into structured token objects
const complexMarkdown = '# Document Heading\n\nParagraph content.\n\n- Item Alpha\n- Item Beta';
const tokens = instance.lexer(complexMarkdown);
assert.ok(Array.isArray(tokens), 'lexer must return an array of tokens');
assert.equal(tokens[0].type, 'heading', 'first token must be heading');
assert.equal(tokens[0].depth, 1, 'heading depth must be 1');
assert.equal(tokens[0].text, 'Document Heading', 'heading text must match');
assert.equal(tokens[1].type, 'space', 'second token must be space');
assert.equal(tokens[2].type, 'paragraph', 'third token must be paragraph');
assert.equal(tokens[4].type, 'list', 'fifth token must be list');
assert.equal(tokens[4].items.length, 2, 'list must contain 2 items');

const helperTokens = tokenizeWithInstance(complexMarkdown, instance);
assert.deepEqual(helperTokens, tokens, 'tokenizeWithInstance helper must match lexer output');

// 6. Marked instance parser method renders token objects into an HTML string
const htmlFromTokens = instance.parser(tokens);
assert.equal(typeof htmlFromTokens, 'string', 'parser output must be a string');
assert.ok(htmlFromTokens.includes('<h1>Document Heading</h1>'), 'parsed tokens must include heading');
assert.ok(htmlFromTokens.includes('<p>Paragraph content.</p>'), 'parsed tokens must include paragraph');
assert.ok(htmlFromTokens.includes('<ul>'), 'parsed tokens must include unordered list');
assert.ok(htmlFromTokens.includes('<li>Item Alpha</li>'), 'parsed tokens must include list items');

const helperCompiled = compileTokensWithInstance(tokens, instance);
assert.equal(helperCompiled, htmlFromTokens, 'compileTokensWithInstance helper must match parser output');

// 7. Marked instance walkTokens method traverses token tree nodes
const visitedTypes = [];
instance.walkTokens(tokens, (token) => {
  visitedTypes.push(token.type);
});
assert.ok(visitedTypes.includes('heading'), 'walkTokens must visit heading token');
assert.ok(visitedTypes.includes('paragraph'), 'walkTokens must visit paragraph token');
assert.ok(visitedTypes.includes('list'), 'walkTokens must visit list token');
assert.ok(visitedTypes.includes('list_item'), 'walkTokens must visit list items');

// 8. Marked instance use method applies extensions without mutating global marked state
const customInstance = new Marked();
customInstance.use({
  renderer: {
    strong({ tokens: strongTokens }) {
      return `<strong class="custom-strong">${this.parser.parseInline(strongTokens)}</strong>`;
    }
  }
});
const customRendered = customInstance.parse('**emphasized**');
assert.ok(
  customRendered.includes('<strong class="custom-strong">emphasized</strong>'),
  'custom instance must apply custom renderer'
);

const defaultRendered = markedModule.marked.parse('**emphasized**');
assert.ok(
  defaultRendered.includes('<p><strong>emphasized</strong></p>'),
  'global marked must retain default renderer without custom class'
);
assert.ok(!defaultRendered.includes('custom-strong'), 'global marked must not be mutated by instance use');

// 9. Marked instance setOptions method modifies instance configuration
const breaksInstance = new Marked();
breaksInstance.setOptions({ breaks: true });
const breaksOutput = breaksInstance.parse('first line\nsecond line');
assert.ok(breaksOutput.includes('<br>'), 'setOptions with breaks: true must insert <br> on single line breaks');

const noBreaksInstance = new Marked();
const noBreaksOutput = noBreaksInstance.parse('first line\nsecond line');
assert.ok(!noBreaksOutput.includes('<br>'), 'default instance must not insert <br> on single line breaks');

console.log('Contract tests passed successfully.');

原始种子者

匿名