CSS, JavaScript, and Regex

 

CSS JS and Regex

With these tools, you can:

  • Customize areas of Aspen
  • Add enhancements
  • Find and modify patterns in code

  

  

  

CSS

Users can add custom CSS to fine-tune the way your catalog looks. If an element exists in the Aspen interface and is not adjustable within the standard Theme settings, chances are you can customize it with CSS.
  1. Visit  Theme & Layout → Themes.
  2. Scroll down to the Additional CSS text box and paste in your CSS code.
  3. By default, the Additional CSS Application option is set to Append to parent CSS. If your changes do not appear, you may need to switch this to Override parent CSS. For most purposes, Append to parent CSS should be fine.

More Information on Themes and CSS

JavaScript

Users can add JavaScript snippets in Aspen Administration > Local Catalog Enrichment > JavaScript Snippets.

Snippets must include script tags <script> </script>

We often see libraries add widgets (for example: chat platforms, Niche Academy) here. Learn More about Chat Platforms here. 

ReciteMe is also something that can be added with a JavaScript Snippet.

Read the Enhancements with JavaScript Aspen Weekly.

Add a JavaScript Snippet

Users can add JavaScript snippets in Aspen Administration > Local Catalog Enrichment > JavaScript Snippets > Add New.

Then, give your snippet a Name (this is for internal use).

Next, add the snippet in the Snippet (include script tags) box. Snippets must include script tags <script> </script>

If your JavaScript Snippet includes analytics cookies, check Contains Analytics Cookies. This will only apply this snippet if cookie consent is given while Require Cookie Consent is enabled. Learn more about Cookies here

To publish them to the catalog you want to apply them to, select Libraries and Locations and save. You should see your changes immediately.

 

Updated 2024-05-02 km bws
Copy a JavaScript Snippet

To copy a JavaScript snippet, start by going to Aspen Administration > Local Catalog Enrichment > JavaScript Snippets > click into the snippet you want to copy.

At the top, click on the Copy button.

 

This will pop open a new JavaScript snippet window with the title Copying {ID of the JavaScript snippet you copied}.

You will need to add a Name, make any adjustments to the code needed (for example if you are targeting a different scoped URL) and then apply to the Libraries and Locations you want.

 

Updated 2024-01-16 km bws

Regular Expressions (Regex)

Regular expressions, or regex, are extremely useful in telling a computer how to find patterns in text, or how to find and replace certain patterns with others. This makes it a bit difficult to read and understand, but writing regex can be easy if you know some of the basics!

Regex Basics

Scoping Catalogs

In Aspen, you may see the expression .* used quite a bit. This regex matches any character, any amount of times, making it all inclusive. If you use this expression in, say, the Locations field in Records to Include, it will include records from all locations.

Regex is sprinkled over a few different settings in Aspen Discovery. The most notable and most used is found in Aspen Administration > Primary Configuration > Library Systems. In this module, you can use regular expressions to specify what records you own and wish to include in your catalog.

Here, you can see the commonly used .* expression in the Location field for Records To Include.

In the Locations to Exclude, we have bookm which is our bookmobile location. If you wanted to add more, all you would need to do is separate them with a pipe. For example: bookm|eastbranch

Self Registration

Aspen also includes customizable regular expression fields for valid zip codes for self registration in Aspen (also found in the Library Systems module).

In this case, there were too many individual zip codes to list, so writing a regular expression made the list much smaller and more efficient.

Translation Maps

Aspen has the ability to integrate with many different underlying ILS platforms. To manage the interpretation of data such as Shelf Location, Audience, and Literary Form (to name a few) Aspen has Translation Maps found in Aspen Administration > ILS Integration > Translation Maps (also found at the bottom of ILS Indexing Profiles). In order to translate a large amount of data on these maps, you can use regex.

In this example, the library is using shelf locations to determine audience by translating these values in their Audience Map. With 4 fields and a bit of regex, they are telling Aspen that anything matching those shelf location codes matches the translated value they have set for audience.

Website Indexing

Yet another common place where regular expressions are used in Aspen is within the Website Indexing module.

The regular expression used above:

<meta name="description" content="(.*?)"/> 

This is an example of regex used to find Description metadata within the indexed pages. This description will display within the Library Website search results.

These regular expressions specify which sites you do not want to be indexed by using part of the path. Ending the regex with .* ensures that everything after /events/ or /admin/ is included in the paths to exclude. So, if you have a page that ends in /events/summertimefun - this page will not be indexed.

Note: if you want to test your regex for this particular field, use a tester for Java 8 regex:

Debugging and Testing Regex

Debugging Regex

Okay, so you’ve written your regex. Everything should be working, but something is amiss and looking at everything you just wrote is absolutely headache-inducing. Have no fear, there’s an easy way to debug your regex! 

There are many regex testers out there to help, including regex101 and Regexr. Below is an example of a regex testing in regex101. The goal with this regex is to match the following numbers only:12340, 12341, 12342, 12343, 12345, 12346, and 12348.

The test strings that matches the regex are highlighted in blue, and different parts of the regex that are highlighted are explained as well.

When using a tester, it’s important to choose the correct “flavor” of programming, as each language has it’s own special rules. Generally, you will want to test with PHP.

A regex tester is a great tool for anyone to use when writing regular expressions, whether you be an expert or just starting out!