Skip to content
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

block defaults #1749

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions observablehq.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default {
root: "docs",
output: "docs/.observablehq/dist",
title: "Observable Framework",
defaultBlockAttributes: {
"*": {
run: "false"
}
},
pages: [
{name: "What is Framework?", path: "/what-is-framework"},
{name: "Getting started", path: "/getting-started"},
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export function normalizeConfig(spec: ConfigSpec = {}, defaultRoot?: string, wat
}

const config: Config = {
defaultBlockAttributes: spec.defaultBlockAttributes, // TODO
root,
output,
base,
Expand Down
18 changes: 16 additions & 2 deletions src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface ParseContext {
currentLine: number;
path: string;
params?: Params;
defaultBlockAttributes?: ParseOptions["defaultBlockAttributes"];
}

function uniqueCodeId(context: ParseContext, content: string): string {
Expand Down Expand Up @@ -114,6 +115,8 @@ function makeFenceRenderer(baseRenderer: RenderRule): RenderRule {
const {path, params} = context;
const token = tokens[idx];
const {tag, attributes} = parseInfo(token.info);
inheritAttributes(attributes, context.defaultBlockAttributes?.[tag]);
inheritAttributes(attributes, context.defaultBlockAttributes?.["*"]);
token.info = tag;
let html = "";
let source: string | undefined;
Expand Down Expand Up @@ -141,6 +144,16 @@ function makeFenceRenderer(baseRenderer: RenderRule): RenderRule {
};
}

function inheritAttributes(attributes: Record<string, string>, defaults?: Record<string, string>): void {
if (defaults) {
for (const key in defaults) {
if (!(key in attributes)) {
attributes[key] = defaults[key];
}
}
}
}

const CODE_DOLLAR = 36;
const CODE_BRACEL = 123;

Expand Down Expand Up @@ -217,6 +230,7 @@ export interface ParseOptions {
header?: Config["header"];
footer?: Config["footer"];
params?: Params;
defaultBlockAttributes?: Record<string, Record<string, string>>;
}

export function createMarkdownIt({
Expand All @@ -242,10 +256,10 @@ export function createMarkdownIt({
}

export function parseMarkdown(input: string, options: ParseOptions): MarkdownPage {
const {md, path, params} = options;
const {md, path, params, defaultBlockAttributes} = options;
const {content, data} = readFrontMatter(input);
const code: MarkdownCode[] = [];
const context: ParseContext = {code, startLine: 0, currentLine: 0, path, params};
const context: ParseContext = {code, startLine: 0, currentLine: 0, path, params, defaultBlockAttributes};
const tokens = md.parse(content, context);
const body = md.renderer.render(tokens, md.options, context); // Note: mutates code!
const title = data.title !== undefined ? data.title : findTitle(tokens);
Expand Down
Loading