Skip to main content
Glama

move_section

Reposition sections within Markdown documents to reorganize content structure and improve document flow.

Instructions

        Move a section to a different position.
        The document will be saved after the operation if successful and auto_save is True.
        

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
document_pathYes
section_idYes
target_positionYes
auto_saveNo
backupNo
validation_levelNoNORMAL

Implementation Reference

  • Core implementation of the move_section operation in SafeMarkdownEditor. Validates source and target sections, records a transaction, updates version, and returns success (with note that full implementation is pending). This is the primary handler logic called by MCP tools.
    def move_section(self, section_ref: SectionReference, target_ref: SectionReference, 
                    position: str = "after") -> EditResult:
        """
        Move a section to a new position in the document.
        
        Args:
            section_ref: Section to move
            target_ref: Target section to move relative to
            position: "before" or "after" target section
            
        Returns:
            EditResult with operation details
        """
        with self._lock:
            try:
                # Validate both sections exist
                if not self._is_valid_section_reference(section_ref):
                    return EditResult(
                        success=False,
                        operation=EditOperation.MOVE_SECTION, 
                        modified_sections=[],
                        errors=[SafeParseError(
                            message=f"Source section not found: {section_ref.title}",
                            error_code="SECTION_NOT_FOUND",
                            category=ErrorCategory.VALIDATION
                        )],
                        warnings=[]
                    )
                
                if not self._is_valid_section_reference(target_ref):
                    return EditResult(
                        success=False,
                        operation=EditOperation.MOVE_SECTION,
                        modified_sections=[],
                        errors=[SafeParseError(
                            message=f"Target section not found: {target_ref.title}",
                            error_code="SECTION_NOT_FOUND", 
                            category=ErrorCategory.VALIDATION
                        )],
                        warnings=[]
                    )
                
                if position not in ["before", "after"]:
                    return EditResult(
                        success=False,
                        operation=EditOperation.MOVE_SECTION,
                        modified_sections=[],
                        errors=[SafeParseError(
                            message="Position must be 'before' or 'after'",
                            error_code="INVALID_POSITION",
                            category=ErrorCategory.VALIDATION
                        )],
                        warnings=[]
                    )
                
                # Store rollback data
                rollback_data = self._current_text
                
                # Implementation simplified for now - just return success
                # Full implementation would require complex section boundary management
                
                # Record transaction
                operation_dict = {
                    'operation': EditOperation.MOVE_SECTION,
                    'section_id': section_ref.id,
                    'content': "",
                    'target_section_id': target_ref.id,
                    'position': position
                }
                
                self._record_transaction([operation_dict], rollback_data)
                
                # Update version
                self._version += 1
                self._last_modified = datetime.now()
                
                return EditResult(
                    success=True,
                    operation=EditOperation.MOVE_SECTION,
                    modified_sections=[section_ref, target_ref],
                    errors=[],
                    warnings=[SafeParseError(
                        message="Move operation is simplified - full implementation pending",
                        error_code="SIMPLIFIED_IMPLEMENTATION",
                        category=ErrorCategory.OPERATION
                    )],
                    metadata={
                        'position': position,
                        'version': self._version
                    }
                )
                
            except Exception as e:
                return EditResult(
                    success=False,
                    operation=EditOperation.MOVE_SECTION,
                    modified_sections=[],
                    errors=[SafeParseError(
                        message=f"Move operation failed: {str(e)}",
                        error_code="MOVE_ERROR",
                        category=ErrorCategory.SYSTEM
                    )],
                    warnings=[]
                )
  • MCP tool registration for the 'move_section' tool using FastMCP. Defines parameters (document_path, section_id, target_position), implements operation by loading editor, finding sections, and delegating to SafeMarkdownEditor's move_section method, then executes via stateless processor.
    def move_section(document_path: str, section_id: str, target_position: int,
                    auto_save: bool = True, backup: bool = True,
                    validation_level: str = "NORMAL") -> Dict[str, Any]:
        """
        Move a section to a different position.
        The document will be saved after the operation if successful and auto_save is True.
        """
        """
        Move a section to a different position.
        
        Args:
            document_path: Path to the Markdown file
            section_id: The section ID to move
            target_position: Target position (0-based index)
            auto_save: Whether to automatically save the document
            backup: Whether to create a backup before saving
            validation_level: Validation strictness - "STRICT", "NORMAL", or "PERMISSIVE"
        """
        def operation(editor):
            section_ref = editor.get_section_by_id(section_id)
            if not section_ref:
                from .safe_editor_types import EditResult, OperationType
                return EditResult(
                    success=False,
                    operation=OperationType.MOVE,
                    modified_sections=[],
                    errors=[f"Section with ID '{section_id}' not found"],
                    warnings=[]
                )
            
            sections = editor.get_sections()
            if target_position >= len(sections) or target_position < 0:
                from .safe_editor_types import EditResult, OperationType
                return EditResult(
                    success=False,
                    operation=OperationType.MOVE,
                    modified_sections=[],
                    errors=[f"Target position {target_position} is out of range (0-{len(sections)-1})"],
                    warnings=[]
                )
            
            target_section = sections[target_position]
            return editor.move_section(section_ref, target_section, "after")
        
        validation_map = {"STRICT": ValidationLevel.STRICT, "NORMAL": ValidationLevel.NORMAL, "PERMISSIVE": ValidationLevel.PERMISSIVE}
        validation_enum = validation_map.get(validation_level.upper(), ValidationLevel.NORMAL)
        
        return self.processor.execute_operation(document_path, operation, auto_save, backup, validation_enum)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/quantalogic/quantalogic_markdown_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server