Altinn / digdir-assistants

Generative AI assistants
MIT License
3 stars 0 forks source link

Fix conversion errors in `@tryfabric/mack` #49

Open bdb-dd opened 6 months ago

bdb-dd commented 6 months ago

Description of the bug

Code blocks are being removed in the current implementation of Mack, specifically code blocks nested in lists.

Here is an example of the current output: image

The input markdown looks like this:

To configure a good policy for your app in Altinn Studio, you need to define the authorization rules in an XACML policy file. This file is placed in the `App/config/authorization` directory of your application repository. Here are the steps and considerations for configuring a good policy:\n\n### Steps to Configure a Policy\n\n1. **Create or Edit the XACML Policy File:**\n   - When you create an app in Altinn Studio, it includes an autogenerated XACML policy file based on the current ASP.NET template. This file is located at `App/config/authorization/policy.xml`.\n   - You can edit this file using a text editor of your choice.\n\n2. **Define Resource Attributes:**\n   - Resource attributes describe which application or part of the application the rules apply to.\n   - Example:\n     ```xml\n     <xacml:AllOf>\n       <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n         <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">skd</xacml:AttributeValue>\n         <xacml:AttributeDesignator AttributeId=\"urn:altinn:org\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n       </xacml:Match>\n       <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n         <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">taxreport</xacml:AttributeValue>\n         <xacml:AttributeDesignator AttributeId=\"urn:altinn:app\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n       </xacml:Match>\n       <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n         <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">instansiate</xacml:AttributeValue>\n         <xacml:AttributeDesignator AttributeId=\"urn:altinn:event\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n       </xacml:Match>\n     </xacml:AllOf>\n     ```\n\n3. **Define Subject Attributes:**\n   - Subject attributes define who the rule targets.\n   - Example with role code:\n     ```xml\n     <xacml:AllOf>\n       <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n         <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">regna</xacml:AttributeValue>\n         <xacml:AttributeDesignator AttributeId=\"urn:altinn:rolecode\" Category=\"urn:oasis:names:tc:xacml:1.0:subject-category:access-subject\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n       </xacml:Match>\n     </xacml:AllOf>\n     ```\n   - Example with org:\n     ```xml\n     <xacml:AllOf>\n       <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n         <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">skd</xacml:AttributeValue>\n         <xacml:AttributeDesignator AttributeId=\"urn:altinn:org\" Category=\"urn:oasis:names:tc:xacml:1.0:subject-category:access-subject\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n       </xacml:Match>\n     </xacml:AllOf>\n     ```\n\n4. **Define Action Attributes:**\n   - Action attributes describe which actions the rules apply to, such as read, write, instantiate, complete, and delete.\n   - Example:\n     ```xml\n     <xacml:AllOf>\n       <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n         <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">read</xacml:AttributeValue>\n         <xacml:AttributeDesignator AttributeId=\"urn:oasis:names:tc:xacml:1.0:action:action-id\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:action\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n       </xacml:Match>\n     </xacml:AllOf>\n     ```\n\n### Considerations for a Good Policy\n\n- **Clarity and Specificity:** Ensure that the rules are clear and specific to avoid any ambiguity in who can access what and perform which actions.\n- **Least Privilege Principle:** Grant the minimum level of access necessary for users to perform their tasks.\n- **Regular Reviews:** Regularly review and update the policy to adapt to any changes in the application or organizational requirements.\n- **Testing:** Thoroughly test the policy to ensure it works as expected and does not inadvertently block legitimate access or allow unauthorized access.\n\n### Example Policy Snippet\n\nHere is a complete example snippet combining resource, subject, and action attributes:\n\n```xml\n<xacml:Policy xmlns:xacml=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\"\n              PolicyId=\"example-policy\"\n              RuleCombiningAlgId=\"urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:permit-overrides\">\n  <xacml:Target>\n    <xacml:AnyOf>\n      <xacml:AllOf>\n        <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n          <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">skd</xacml:AttributeValue>\n          <xacml:AttributeDesignator AttributeId=\"urn:altinn:org\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n        </xacml:Match>\n        <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n          <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">taxreport</xacml:AttributeValue>\n          <xacml:AttributeDesignator AttributeId=\"urn:altinn:app\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n        </xacml:Match>\n        <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n          <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">read</xacml:AttributeValue>\n          <xacml:AttributeDesignator AttributeId=\"urn:oasis:names:tc:xacml:1.0:action:action-id\" Category=\"urn:oasis:names:tc:xacml:3.0:attribute-category:action\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n        </xacml:Match>\n        <xacml:Match MatchId=\"urn:oasis:names:tc:xacml:1.0:function:string-equal\">\n          <xacml:AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">regna</xacml:AttributeValue>\n          <xacml:AttributeDesignator AttributeId=\"urn:altinn:rolecode\" Category=\"urn:oasis:names:tc:xacml:1.0:subject-category:access-subject\" DataType=\"http://www.w3.org/2001/XMLSchema#string\" MustBePresent=\"false\"/>\n        </xacml:Match>\n      </xacml:AllOf>\n    </xacml:AnyOf>\n  </xacml:Target>\n  <xacml:Rule RuleId=\"permit-rule\" Effect=\"Permit\">\n    <xacml:Target/>\n  </xacml:Rule>\n</xacml:Policy>\n```\n\nBy following these steps and considerations, you can configure a robust and effective authorization policy for your app in Altinn Studio.\n\n### References\n- [Altinn Studio Authorization Documentation](https://docs.altinn.studio/app/development/configuration/authorization/)\n- [XACML Policy Example](https://raw.githubusercontent.com/Altinn/altinn-studio/master/src/studio/AppTemplates/AspNet/App/config/authorization/policy.xml)

Steps To Reproduce

  1. Ask Assistant a question that is likely to include multiple code blocks, such as "how do I configure a good policy for my app?".
  2. Notice that there are few code blocks, and none that are nested in a list.

Additional Information

No response

### Tasks
- [x] Create a new section for each token type
- [ ] Merge sibling sections that are the same token type
bdb-dd commented 6 months ago

Running into max number of blocks in Slack message (50), need to avoid unnecessary sections when possible