Skip to content

fix(to-markdown): indent container contens #93

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: corepack enable
- run: npm i -g --force corepack && corepack enable
- run: pnpm install
- run: pnpm dev:prepare
- run: pnpm lint
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export default <Plugin<Array<RemarkMDCOptions>>> function remarkMDC(opts: Remark
if (opts.yamlCodeBlockProps === undefined && opts.experimental?.componentCodeBlockYamlProps) {
opts.yamlCodeBlockProps = opts.experimental.componentCodeBlockYamlProps
}
if (typeof opts.indentContent === 'undefined') {
opts.indentContent = true
}

add('micromarkExtensions', syntax())
add('fromMarkdownExtensions', fromMarkdown(opts))
Expand Down
26 changes: 17 additions & 9 deletions src/to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ function compilePattern(pattern: Unsafe) {
type NodeComponentContainerSection = Parents & { name: string }

export default (opts: RemarkMDCOptions = {}) => {
const indentedContent = (node: any, context: State) => {
if (opts.indentContent) {
return content(node, context).split('\n')
.map(line => line.length > 0 ? ' ' + line : line)
.join('\n')
}
return content(node, context)
}
const applyAutomaticUnwrap = (node: Container, { safeTypes = [] }: Exclude<RemarkMDCOptions['autoUnwrap'], boolean | undefined>) => {
const isSafe = (type: string) => NON_UNWRAPPABLE_TYPES.has(type) || safeTypes.includes(type)
if (!node.mdc?.unwrapped) {
Expand Down Expand Up @@ -97,7 +105,9 @@ export default (opts: RemarkMDCOptions = {}) => {

processNode(node as any)

return `#${(node as any).name}${attributes(node, context)}\n${content(node, context)}`.trim()
const subvalue = indentedContent(node, context)

return `#${(node as any).name}${attributes(node, context)}\n${subvalue}`.trim()
}

type NodeTextComponent = Parents & { name: string, rawData: string, attributes: any }
Expand Down Expand Up @@ -164,18 +174,16 @@ export default (opts: RemarkMDCOptions = {}) => {

let subvalue
if ((node.type as string) === 'containerComponent') {
subvalue = content(node, context)
subvalue = node.children.map((child) => {
return child.type === 'componentContainerSection'
? content({ children: [child] }, context)
: indentedContent({ children: [child] }, context)
}).join('\n\n')

if (subvalue) {
value += '\n' + subvalue
}
value += '\n' + prefix

if (nest > 1) {
value = value
.split('\n')
.map(line => ' ' + line)
.join('\n')
}
}
nest -= 1
exit()
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface RemarkMDCOptions {
safeTypes?: Array<string>
}
yamlCodeBlockProps?: boolean
indentContent?: boolean
experimental?: {
/**
* @deprecated This feature is out of experimental, use `autoUnwrap`
Expand Down
Loading
Loading