/*
 * Copyright (c) 2018, 2026, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const ruleDef=function(){};ruleDef.prototype.getName=function(){return"jetwc-markdown-link"};ruleDef.prototype.getShortDescription=function(){return"Validates any links in markdown documents"};ruleDef.prototype.getDescription=function(){return"This rule flags any links used in markdown based documents such as README.md or CHANGELOG.md which are not in the correct markdown format. It also allows verification of any servers against include or exclude globs so that links can be sanitised if required"};ruleDef.prototype.register=function(context){return{startaudit:this._auditPrep,"md-link":this._doAudit}};ruleDef.prototype._auditPrep=function(context){let allowList=context.rulePack.getRuleCustomOption("allow");let disallowList=context.rulePack.getRuleCustomOption("disallow");this.urlMatchExpressions=[];this.allowMatching=true;let processList=allowList;if(!processList){processList=disallowList;this.allowMatching=false}if(processList){processList.forEach(pattern=>{this.urlMatchExpressions.push(new RegExp(pattern,"i"))})}};ruleDef.prototype._doAudit=function(context,linkObj){let issue;let matchCount=0;this.urlMatchExpressions.forEach(urlRegex=>{if(urlRegex.test(linkObj.link)){matchCount++}});if(this.allowMatching&&matchCount===0){issue=new context.Issue(`Hyperlink '${linkObj.link}' does not match an allowed link pattern`);issue.setMsgKey(context.ruleName+"_2");issue.setSeverity("blocker")}else if(!this.allowMatching&&matchCount>0){issue=new context.Issue(`Hyperlink '${linkObj.link}' matches a disallowed link pattern`);issue.setMsgKey(context.ruleName+"_2");issue.setSeverity("blocker")}if(!issue&&!linkObj.text){issue=new context.Issue(`Hyperlink '${linkObj.link}' is missing an associated label in the markdown`);issue.setMsgKey(context.ruleName+"_1")}if(issue){issue.setPosition(linkObj.pos);context.reporter.addIssue(issue,context)}};module.exports=ruleDef;