MCPcopy
hub / github.com/markdoc/markdoc / formatNode

Function formatNode

src/formatter.ts:143–461  ·  view source on GitHub ↗
(n: Node, o: Options = {})

Source from the content-addressed store, hash-verified

141}
142
143function* formatNode(n: Node, o: Options = {}) {
144 const no = { ...o, parent: n };
145 const indent = SPACE.repeat(no.indent || 0);
146
147 switch (n.type) {
148 case 'document': {
149 if (n.attributes.frontmatter && n.attributes.frontmatter.length) {
150 yield '---' + NL + n.attributes.frontmatter + NL + '---' + NL + NL;
151 }
152 yield* trimStart(formatChildren(n, no));
153 break;
154 }
155 case 'heading': {
156 yield NL;
157 yield indent;
158 yield '#'.repeat(n.attributes.level || 1);
159 yield SPACE;
160 yield* trimStart(formatChildren(n, no));
161 yield* formatAnnotations(n);
162 yield NL;
163 break;
164 }
165 case 'paragraph': {
166 yield NL;
167 yield* formatChildren(n, no);
168 yield* formatAnnotations(n);
169 yield NL;
170 break;
171 }
172 case 'inline': {
173 yield indent;
174 yield* formatChildren(n, no);
175 break;
176 }
177 case 'image': {
178 yield '!';
179 yield '[';
180 yield* formatValue(n.attributes.alt, no);
181 yield ']';
182 yield '(';
183 yield* typeof n.attributes.src === 'string'
184 ? escapeMarkdownCharacters(n.attributes.src, /[()]/g)
185 : formatValue(n.attributes.src, no);
186 if (n.attributes.title) {
187 yield SPACE + `"${n.attributes.title}"`;
188 }
189 yield ')';
190 break;
191 }
192 case 'link': {
193 const children = [...formatChildren(n, no)].join('');
194
195 // https://spec.commonmark.org/0.31.2/#autolinks
196 if (children === n.attributes.href && !n.attributes.title) {
197 yield `<${n.attributes.href}>`;
198 break;
199 }
200

Callers 1

formatValueFunction · 0.85

Calls 10

trimStartFunction · 0.85
formatChildrenFunction · 0.85
formatAnnotationsFunction · 0.85
formatValueFunction · 0.85
escapeMarkdownCharactersFunction · 0.85
formatFunction · 0.85
formatAttributesFunction · 0.85
incrementFunction · 0.85
formatInlineFunction · 0.85
formatTableRowFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…