Skip to content
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

Rich Indexing #3113

Open
wants to merge 80 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
7dc6eae
Baseline Index Anchors Code
dbolacksn Oct 25, 2023
764c461
Initial Walking of the index.
dbolacksn Oct 26, 2023
7b9557a
Working index generation
dbolacksn Oct 27, 2023
d6d0c1e
First working draft of rich indexes.
dbolacksn Oct 27, 2023
9c90328
Add case insensitive index sorting.
dbolacksn Oct 27, 2023
5c0707e
Remove unneeded comments.
dbolacksn Oct 27, 2023
55e2fc5
Correct updated markdown test.
dbolacksn Oct 27, 2023
cb37509
Update page jump anchors based on HTML feature I somehow never learned!
dbolacksn Oct 27, 2023
9c4252e
Merge branch 'master' into Indexing
dbolacksn Dec 6, 2023
e2f2e7d
Try and solve conflict?
dbolacksn Dec 14, 2023
8c33798
Unsure why these merge conflicts were yelling at me.
dbolacksn Dec 14, 2023
362abde
Merge branch 'master' into Indexing
dbolack-ab Jan 5, 2024
ec3dd3f
Fix merge error
dbolacksn Jan 6, 2024
1588385
Update Index tagging to mimic the reflink pattern.
dbolacksn Jan 10, 2024
af33989
Update tests for Rich Indexes.
dbolacksn Jan 10, 2024
46c6f16
Change terms and corresponding variable names
dbolacksn Jan 10, 2024
6e4021d
Heavy rework to better allow for combined indexes.
dbolacksn Jan 13, 2024
ebc6a7d
Update Rich Indexing Tests
dbolacksn Jan 13, 2024
fb37ff8
Set pagenumber assuming PR #3144 is merged.
dbolacksn Jan 13, 2024
47aaf54
Update link creation to use brew page numbers, 1 based.
dbolacksn Jan 16, 2024
f1b21af
Merge branch 'master' into Indexing
dbolacksn Jan 16, 2024
cbb1f5a
Merge branch 'master' into Indexing
dbolack-ab Jan 20, 2024
8e7a54d
Merge branch 'master' into Indexing
dbolack-ab Jan 30, 2024
df8e6d0
Merge branch 'master' into Indexing
dbolack-ab Feb 7, 2024
9738c6c
Merge branch 'master' into Indexing
dbolack-ab Feb 8, 2024
10fd1aa
Merge branch 'master' into Indexing
dbolacksn Mar 4, 2024
2499ce3
Fix the merge to work though baffled as to why it didn't before...
dbolacksn Mar 4, 2024
d334cee
Merge branch 'Indexing' of github.com:dbolacksn/homebrewery-broken in…
dbolacksn Mar 4, 2024
7bf6ede
Merge branch 'master' into Indexing
dbolacksn Mar 7, 2024
bae8b3f
Finish inversion of Index method.
dbolacksn Mar 7, 2024
8479794
Partial fix on Rich Indexes revamp
dbolacksn Mar 7, 2024
6387a31
Remaining adjustment cleanup.
dbolacksn Mar 8, 2024
3124d45
Sorta working.
dbolacksn Mar 10, 2024
97658c1
Merge branch 'master' into Indexing
dbolacksn Mar 20, 2024
54f6e6c
Convert to newly suggested syntax.
dbolacksn Mar 22, 2024
bea78a4
Begin adapting snippet to new logic and layout
dbolacksn Mar 23, 2024
a49f4cf
Merge branch 'master' into Indexing
dbolacksn Mar 23, 2024
6bd1527
Continued rework to current moving target
dbolacksn Mar 30, 2024
3c8b9c3
Implement Snippet Index Collection
dbolacksn Mar 31, 2024
cf74913
Continued Progress reqorking Rich Index Snippet
dbolacksn Mar 31, 2024
ef9227c
Sorted Indexes, correct page omissions
dbolacksn Mar 31, 2024
dac4708
Clean up a few console.logs
dbolacksn Apr 1, 2024
f291720
Fix index address chomping for references.
dbolacksn Apr 1, 2024
64847ae
Working anchor placement and or, eating.
dbolacksn Apr 10, 2024
e0d9a63
Console.log and comments cleanup that I thought was saved in the last
dbolacksn Apr 10, 2024
a2bcade
Completed RichIndex Snippet updates
dbolacksn Apr 11, 2024
9282248
Completed snippet rework.
dbolacksn Apr 20, 2024
5853e93
Nearly done working copy
dbolacksn Apr 20, 2024
ac0a845
Merge branch 'master' into Indexing
dbolacksn Apr 21, 2024
28e9353
Update tests
dbolacksn Apr 21, 2024
db10ecd
Merge branch 'master' into Indexing
dbolacksn May 1, 2024
55f1216
Strip paragraph wrappers on Index related blocks
dbolacksn May 1, 2024
0dee6d5
Update tests
dbolacksn May 1, 2024
1340cd8
Merge branch 'master' into Indexing
dbolacksn May 6, 2024
cf5f1e0
Merge branch 'master' into Indexing
dbolacksn May 12, 2024
3174caa
Merge branch 'master' into Indexing
dbolacksn May 20, 2024
e7cf8dc
Merge branch 'master' into Indexing
dbolacksn Jun 1, 2024
5d66593
Merge branch 'master' into Indexing
dbolacksn Aug 1, 2024
d5880c8
Merge branch 'master' into Indexing
dbolacksn Aug 2, 2024
99ca009
Merge branch 'master' into Indexing
dbolacksn Aug 13, 2024
33c2707
Merge branch 'master' into Indexing
dbolacksn Aug 29, 2024
55f6617
Update tests to match expected page references.
dbolacksn Aug 29, 2024
e4a74ca
Merge branch 'master' into Indexing
dbolacksn Aug 31, 2024
08d6021
Clean up small typo reintroduced by merge
dbolacksn Aug 31, 2024
8e5806e
Merge branch 'master' into Indexing
dbolacksn Sep 15, 2024
774fc75
Merge branch 'master' into Indexing
dbolacksn Sep 16, 2024
169a74a
Merge branch 'master' into Indexing
dbolacksn Sep 18, 2024
5fcb9ad
Fix a couple of weird corner cases that should have already been noti…
dbolacksn Sep 24, 2024
98f876b
Fix Test breakage caused be edgecase fix.
dbolacksn Sep 24, 2024
49d3b6c
Merge branch 'master' into Indexing
dbolacksn Sep 24, 2024
6fa3392
Fix bad regex in planting Index based anchors.
dbolacksn Sep 24, 2024
6649726
Merge branch 'master' into Indexing
dbolacksn Oct 7, 2024
56b838a
Merge branch 'master' into Indexing
dbolacksn Oct 14, 2024
d6727a9
Merge branch 'master' into Indexing
dbolacksn Oct 15, 2024
c905124
Convert silly concats to a proper join.
dbolacksn Oct 16, 2024
eb03229
Merge branch 'master' into Indexing
dbolacksn Oct 28, 2024
34724ad
Merge branch 'master' into Indexing
dbolacksn Nov 12, 2024
f9cac36
Merge branch 'master' into Indexing
dbolacksn Dec 5, 2024
86f9a8a
Merge branch 'master' into Indexing
dbolacksn Dec 20, 2024
e58cdd2
Merge branch 'master' into Indexing
dbolacksn Dec 28, 2024
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
9 changes: 9 additions & 0 deletions client/homebrew/editor/editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ const Editor = createClass({
}
}

// Rich Indexes
if(line.includes('@[')){
const regex = /@\[((?:\\.|[^\[\]\\^@^\)])*)\]\(((?:\\.|[^\[\]\\^@^\)])*)\)/ym;
let match;
while ((match = regex.exec(line)) != null){
codeMirror.markText({ line: lineNumber, ch: line.indexOf(match[0]) - 3 }, { line: lineNumber, ch: 1 }, { className: 'cm-image cm-image-marker' });
}
}

// Subscript & Superscript
if(line.includes('^')) {
let startIndex = line.indexOf('^');
Expand Down
108 changes: 107 additions & 1 deletion shared/naturalcrit/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ renderer.html = function (html) {
// Don't wrap {{ Spans alone on a line, or {{ Divs in <p> tags
renderer.paragraph = function(text){
let match;
// Strip empty paragraphs
if(text.trim() == '') return '';
// Don't wrap index anchors
if(text.startsWith('<a') && (text.indexOf('data-topic') > 0)) return text;
if(text.startsWith('<div') || text.startsWith('</div'))
return `${text}`;
else if(match = text.match(/(^|^.*?\n)<span class="inline-block(.*?<\/span>)$/)) {
Expand Down Expand Up @@ -342,6 +346,107 @@ const mustacheInjectBlock = {
}
};

const indexesDuplicates = (indexEntry)=>{
let count = 0;
for (const ie of indexes) {
if((ie.topic === indexEntry.topic) && (ie.subtopic === indexEntry.subtopic) && (ie.index === indexEntry.index)) {
count++;
}
}
indexes.push({ ...indexEntry });
return count == 0 ? '' : count;
};

const indexSplit=(src)=>{
let index, topic, subtopic;
const indexSplitRegex = /(?<!\\):/;
const subTopicSplit = /(?<!\\)\//;

let working = [];
if(src.search(indexSplitRegex) < 0){
working[1] = src.trim();
index = 'Index:';
} else {
working = src.split(indexSplitRegex);
index = working[0].replace('\\:', ':').trim();
if(!working[1]?.trim()>0) {
working.splice(1, 1);
}
working[1] = working[1]?.trim();
}

if(working[1]) {
if(working[1].search(subTopicSplit) !== -1){
const topics = working[1].split(subTopicSplit);
topic = topics[0].trim();
if(topics[1]) { topics[1] = topics[1].trim(); }
if(topics[1]?.length>0) {
subtopic = topics[1].trim();
}
} else {
topic = working[1];
}
}

if(topic?.length>0) {
return { index: index, topic: topic, subtopic: subtopic };
} else {
return undefined;
}

};

const indexAnchors = {
name : 'indexAnchor',
level : 'block',
start(src) {return src.match(/^#(.+)(?<!\\):([^/]+)((?<!\\)\/([^|]+))?\n/)?.index;}, // Hint to Marked.js to stop and check for a match
tokenizer(src, tokens) {
// const inlineRegex = /^#((.+)(?<!\\):)?([^/]+)((?<!\\)\/([^|]+))?\n/gmy;
const inlineRegex = /^#((.+)(?<!\\):)?(.+)((?:(?<!\\)\/(.+)))?\n/gmy;

const indexEntry = {};

let srcMatch;
while (srcMatch = inlineRegex.exec(src)){
const crossReferenceSplit = /(?<!\\)\|/g;

const crossReference = srcMatch[0].split(crossReferenceSplit);
let entryMatch = undefined;
if((crossReference[0][1] !== '#') && (crossReference[0][1] !== ' ')) {

entryMatch = indexSplit(crossReference[0].slice(1));
}
if(!entryMatch) { return; }
indexEntry.subtopic = entryMatch?.subtopic ? entryMatch.subtopic : undefined;
indexEntry.topic = entryMatch.topic;
indexEntry.index = entryMatch.index;
indexEntry.instance = indexesDuplicates(indexEntry);
if(crossReference.length > 1) {
indexEntry.crossReference = true;
}
return {
type : 'indexAnchor',
text : src,
raw : srcMatch[0],
pageNumber : globalPageNumber,
indexEntry : indexEntry
};
}
},
renderer(token) {
if(!token.indexEntry?.crossReference) {
if(token.indexEntry?.subtopic) {
// This is a Subtopic entry
return `<a id="p${token.pageNumber}_${token.indexEntry.subtopic.replace(/\s/g, '').toLowerCase()}${token.indexEntry.instance}" data-topic="${token.indexEntry.topic}" data-subtopic="${token.indexEntry.subtopic}" data-index="${token.indexEntry.index}"></a>`;
} else {
// This is a Topic entry
return `<a id="p${token.pageNumber}_${token.indexEntry.topic.replace(/\s/g, '').replace(/\|/g, '_').toLowerCase()}${token.indexEntry.instance}" data-topic="${token.indexEntry.topic}" data-index="${token.indexEntry.index}"></a>`;
}
}
return '';
}
};

const superSubScripts = {
name : 'superSubScript',
level : 'inline',
Expand Down Expand Up @@ -769,7 +874,7 @@ const tableTerminators = [
];

Marked.use(MarkedVariables());
Marked.use({ extensions : [definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks,
Marked.use({ extensions : [indexAnchors, definitionListsMultiLine, definitionListsSingleLine, forcedParagraphBreaks,
nonbreakingSpaces, superSubScripts, mustacheSpans, mustacheDivs, mustacheInjectInline] });
Marked.use(mustacheInjectBlock);
Marked.use({ renderer: renderer, tokenizer: tokenizer, mangle: false });
Expand Down Expand Up @@ -875,6 +980,7 @@ const extractHTMLStyleTags = (htmlString)=>{
const globalVarsList = {};
let varsQueue = [];
let globalPageNumber = 0;
let indexes = [];

const Markdown = {
marked : Marked,
Expand Down
50 changes: 50 additions & 0 deletions tests/markdown/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,53 @@ test('Check markdown is using the custom renderer; specifically that it adds tar
const rendered = Markdown.render(source);
expect(rendered).toBe('<div> <p><a href="#p1" target="_self">Has _self Attribute?</a></p>\n </div>');
});

test('Check Using Index Anchor. No Index, A Topic, No Subtopic', function() {
const source=`#there\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('<a id="p0_there" data-topic="there" data-index="Index:"></a>');
});

test('Check Using Index Anchor. No Index, A Topic, A Subtopic', function() {
const source=`#there/hereweare\n\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('<a id="p0_hereweare" data-topic="there" data-subtopic="hereweare" data-index="Index:"></a>');
});

test('Check Using Index Anchor. An Index, A Topic, No Subtopic', function() {
const source=`#My Index:there\n\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('<a id="p0_there" data-topic="there" data-index="My Index"></a>');
});

test('Check Using Index Anchor. An Index, A Topic, A Subtopic', function() {
const source=`#My Index:there/hereweare\n\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('<a id="p0_hereweare" data-topic="there" data-subtopic="hereweare" data-index="My Index"></a>');
});

test('Check Using Index Anchor. An Index, A Topic, A Subtopic - Crossreferenced with No Index, A Topic, No Subtopic', function() {
const source=`#My Index:there/hereweare|Crossreference\n\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('');
});

test('Check Using Index Anchor. An Index, A Topic, A Subtopic - Crossreferenced with No Index, A Topic, A Subtopic', function() {
const source=`#My Index:there/hereweare|Crossreference/CrossSub\n\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('');
});

test('Check Using Index Anchor. An Index, A Topic, A Subtopic - Crossreferenced with An Index, A Topic, No Subtopic', function() {
const source=`#My Index:there/hereweare|Cross Index:Crossreference\n\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('');
});

test('Check Using Index Anchor. An Index, A Topic, A Subtopic - Crossreferenced with An Index, A Topic, A Subtopic', function() {
const source=`#My Index:there/hereweare|Cross Index:Crossreference/CrossSub\n\n`;
const rendered = Markdown.render(source);
expect(rendered).toBe('');
});


7 changes: 7 additions & 0 deletions themes/V3/Blank/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const WatercolorGen = require('./snippets/watercolor.gen.js');
const ImageMaskGen = require('./snippets/imageMask.gen.js');
const FooterGen = require('./snippets/footer.gen.js');
const dedent = require('dedent-tabs').default;
const richIndexGen = require('./snippets/richIndex.gen.js');

module.exports = [

Expand Down Expand Up @@ -127,6 +128,12 @@ module.exports = [
icon : 'fas fa-code',
gen : '<!-- This is a comment that will not be rendered into your brew. Hotkey (Ctrl/Cmd + /). -->'
},
{
name : 'Rich Index',
icon : 'fas fa-indent',
gen : richIndexGen,
experimental : true
},
{
name : 'Homebrewery Credit',
icon : 'fas fa-dice-d20',
Expand Down
Loading