antfu / markdown-it-mdc

MDC (Markdown Components) syntax for markdown-it.
MIT License
101 stars 3 forks source link

feat: strip paragraphs option #2

Closed nobkd closed 11 months ago

nobkd commented 1 year ago

Description

This PR introduces an option to strip / not strip paragraphs from MDC block slots.

nobkd commented 11 months ago

Oh. Just because I noticed it, remark-mdc has paragraphs not stripped by default.

Show code **`package.json`** ```json { "name": "test-paragraph-stripping-remark-mdc", "version": "0.0.0", "main": "index.js", "type": "module", "scripts": { "dev": "node index.js" }, "devDependencies": { "remark-mdc": "^2.0.0", "remark-parse": "^10.0.2", "unified": "^11.0.3", "util": "^0.12.5" } } ``` **`index.js`** ```js import { unified } from 'unified'; import parse from 'remark-parse'; import remarkMDC from 'remark-mdc'; import util from 'util'; function compiler() { this.Compiler = function(root) { return root; } } async function markdownToAST(markdown) { let stream = unified() .use(parse) .use(remarkMDC) .use(compiler); const file = await stream.process(markdown); return file.result; } console.log(util.inspect(await markdownToAST('::component-name\nparagraph one.\n\nparagraph two.\n::'), {showHidden: false, depth: null, colors: true})) ```

Show result ```js { type: 'root', children: [ { type: 'containerComponent', name: 'component-name', attributes: {}, children: [ { type: 'paragraph', children: [ { type: 'text', value: 'paragraph one.', position: { start: { line: 2, column: 1, offset: 17 }, end: { line: 2, column: 15, offset: 31 } } } ], position: { start: { line: 2, column: 1, offset: 17 }, end: { line: 2, column: 15, offset: 31 } } }, { type: 'paragraph', children: [ { type: 'text', value: 'paragraph two.', position: { start: { line: 4, column: 1, offset: 33 }, end: { line: 4, column: 15, offset: 47 } } } ], position: { start: { line: 4, column: 1, offset: 33 }, end: { line: 4, column: 15, offset: 47 } } } ], position: { start: { line: 1, column: 1, offset: 0 }, end: { line: 5, column: 3, offset: 50 } }, data: { hName: 'component-name', hProperties: {} }, fmAttributes: {} } ], position: { start: { line: 1, column: 1, offset: 0 }, end: { line: 5, column: 3, offset: 50 } } } ```
antfu commented 11 months ago

/cc @Atinux @farnabaz - Should we mention it in the spec, or should we introduce a syntax to toggle this for each block? Having the config globally might not be the most intuitive way to me.

nobkd commented 11 months ago

It would be great to have more granular control, but I would like it as an addition to a global default (or keeping paragraphs by default)

Maybe something similar to the unwrap option of nuxt-mdc's MDCSlot?

atinux commented 11 months ago

I have been talking to Ahad about this and I believe we should unwrap by default if the content inside the component contains only one parent.

This gives the possibility to leverage only <slot> for components to be compatible with other data sources and avoid the usage of custom components such as <MDCSlot> (which is not possible when using markdown-it)

We also discussed about the ability to unwrap directly inside at the slot level:

::my-component
#default{unwrap="p"}
My content unwrapped
::
nobkd commented 11 months ago

Moved this changes to a different branch and the PR therefore autoclosed.